From 36f5816af9a517e00aeb66c3cd2355d7fe6a4242 Mon Sep 17 00:00:00 2001 From: Ivan Pusic Date: Tue, 6 Dec 2016 01:20:37 +0100 Subject: [PATCH] (Android) Ask for required permissions --- .../ivpusic/imagepicker/PickerModule.java | 77 ++++++++++++------- package.json | 2 +- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java b/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java index e76c1b1..0e019b3 100644 --- a/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java +++ b/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java @@ -39,7 +39,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; -import java.util.Collections; +import java.util.Arrays; import java.util.List; import java.util.UUID; import java.util.concurrent.Callable; @@ -131,16 +131,23 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi return; } - try { - File file = new File(module.getTmpDir()); - if (!file.exists()) throw new Exception("File does not exist"); + permissionsCheck(activity, promise, Arrays.asList(Manifest.permission.WRITE_EXTERNAL_STORAGE), new Callable() { + @Override + public Void call() throws Exception { + try { + File file = new File(module.getTmpDir()); + if (!file.exists()) throw new Exception("File does not exist"); - module.deleteRecursive(file); - promise.resolve(null); - } catch (Exception ex) { - ex.printStackTrace(); - promise.reject(E_ERROR_WHILE_CLEANING_FILES, ex.getMessage()); - } + module.deleteRecursive(file); + promise.resolve(null); + } catch (Exception ex) { + ex.printStackTrace(); + promise.reject(E_ERROR_WHILE_CLEANING_FILES, ex.getMessage()); + } + + return null; + } + }); } @ReactMethod @@ -158,22 +165,29 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi return; } - try { - String path = pathToDelete; - final String filePrefix = "file://"; - if (path.startsWith(filePrefix)) { - path = path.substring(filePrefix.length()); + permissionsCheck(activity, promise, Arrays.asList(Manifest.permission.WRITE_EXTERNAL_STORAGE), new Callable() { + @Override + public Void call() throws Exception { + try { + String path = pathToDelete; + final String filePrefix = "file://"; + if (path.startsWith(filePrefix)) { + path = path.substring(filePrefix.length()); + } + + File file = new File(path); + if (!file.exists()) throw new Exception("File does not exist. Path: " + path); + + module.deleteRecursive(file); + promise.resolve(null); + } catch (Exception ex) { + ex.printStackTrace(); + promise.reject(E_ERROR_WHILE_CLEANING_FILES, ex.getMessage()); + } + + return null; } - - File file = new File(path); - if (!file.exists()) throw new Exception("File does not exist. Path: " + path); - - module.deleteRecursive(file); - promise.resolve(null); - } catch (Exception ex) { - ex.printStackTrace(); - promise.reject(E_ERROR_WHILE_CLEANING_FILES, ex.getMessage()); - } + }); } private void permissionsCheck(final Activity activity, final Promise promise, final List requiredPermissions, final Callable callback) { @@ -242,7 +256,7 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi setConfiguration(options); mPickerPromise = promise; - permissionsCheck(activity, promise, Collections.singletonList(Manifest.permission.CAMERA), new Callable() { + permissionsCheck(activity, promise, Arrays.asList(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE), new Callable() { @Override public Void call() throws Exception { initiateCamera(activity); @@ -313,7 +327,14 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi setConfiguration(options); mPickerPromise = promise; - initiatePicker(activity); + + permissionsCheck(activity, promise, Arrays.asList(Manifest.permission.WRITE_EXTERNAL_STORAGE), new Callable() { + @Override + public Void call() throws Exception { + initiatePicker(activity); + return null; + } + }); } private String getBase64StringFromFile(String absoluteFilePath) { @@ -589,4 +610,4 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi return image; } -} +} \ No newline at end of file diff --git a/package.json b/package.json index 993b794..7779891 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-image-crop-picker", - "version": "0.10.6", + "version": "0.10.7", "description": "Select single or multiple images, with croping option", "main": "index.js", "scripts": {