Commit 08ee1ad8970c02b8ef1e5d0dbe926643952a4c0a

Authored by Andrei Pfeiffer
1 parent e04739d8
Exists in master

Remember initial intent type after permission was granted

android/src/main/java/com/rncustomwebview/CustomWebViewModule.java
... ... @@ -42,6 +42,7 @@ public class CustomWebViewModule extends ReactContextBaseJavaModule implements A
42 42 private CustomWebViewPackage aPackage;
43 43 private ValueCallback<Uri[]> filePathCallback;
44 44 private Uri outputFileUri;
  45 + private String intentTypeAfterPermissionGranted;
45 46  
46 47 // @todo this could be configured from JS
47 48 final String[] DEFAULT_MIME_TYPES = {"image/*", "video/*", "audio/*"};
... ... @@ -143,9 +144,9 @@ public class CustomWebViewModule extends ReactContextBaseJavaModule implements A
143 144 @Override
144 145 public void onClick(DialogInterface dialog, int item) {
145 146 if (items[item].equals(TAKE_PHOTO)) {
146   - startCamera(MediaStore.ACTION_IMAGE_CAPTURE, "image-", ".jpg");
  147 + startCamera(MediaStore.ACTION_IMAGE_CAPTURE);
147 148 } else if (items[item].equals(TAKE_VIDEO)) {
148   - startCamera(MediaStore.ACTION_VIDEO_CAPTURE, "video-", ".mp4");
  149 + startCamera(MediaStore.ACTION_VIDEO_CAPTURE);
149 150 } else if (items[item].equals(CHOOSE_FILE)) {
150 151 startFileChooser(fileChooserParams);
151 152 } else if (items[item].equals(CANCEL)) {
... ... @@ -171,7 +172,7 @@ public class CustomWebViewModule extends ReactContextBaseJavaModule implements A
171 172 switch (requestCode) {
172 173 case REQUEST_CAMERA: {
173 174 if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
174   - startCamera(MediaStore.ACTION_IMAGE_CAPTURE, "image-", ".jpg");
  175 + startCamera(intentTypeAfterPermissionGranted);
175 176 break;
176 177 }
177 178 else {
... ... @@ -199,24 +200,18 @@ public class CustomWebViewModule extends ReactContextBaseJavaModule implements A
199 200 return ActivityCompat.checkSelfPermission(getCurrentActivity(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
200 201 }
201 202  
202   -
203   - private void startCamera(String intentType, String prefix, String suffix) {
  203 + private void startCamera(String intentType) {
204 204 if (permissionsGranted()) {
205 205 // bring up a camera picker intent
206 206 // we need to pass a filename for the file to be saved to
207 207 Intent intent = new Intent(intentType);
208 208  
209 209 // Create the File where the photo should go
210   - try {
211   - String packageName = getReactApplicationContext().getPackageName();
212   - File capturedFile = createCapturedFile(prefix, suffix);
213   - outputFileUri = FileProvider.getUriForFile(getReactApplicationContext(), packageName+".fileprovider", capturedFile);
214   - intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
215   - getCurrentActivity().startActivityForResult(intent, REQUEST_CAMERA);
216   - } catch (IOException ex) {
217   - Log.e("CREATE FILE", "Error occurred while creating the File", ex);
218   - }
  210 + outputFileUri = getOutputFilename(intentType);
  211 + intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
  212 + getCurrentActivity().startActivityForResult(intent, REQUEST_CAMERA);
219 213 } else {
  214 + intentTypeAfterPermissionGranted = intentType;
220 215 requestPermissions();
221 216 }
222 217 }
... ... @@ -241,7 +236,7 @@ public class CustomWebViewModule extends ReactContextBaseJavaModule implements A
241 236 File storageDir = getReactApplicationContext().getExternalFilesDir(null);
242 237 return File.createTempFile(imageFileName, suffix, storageDir);
243 238 }
244   -
  239 +
245 240 private CharSequence[] getDialogItems(String[] types) {
246 241 List<String> listItems = new ArrayList<String>();
247 242  
... ... @@ -294,6 +289,29 @@ public class CustomWebViewModule extends ReactContextBaseJavaModule implements A
294 289 return types;
295 290 }
296 291  
  292 + private Uri getOutputFilename(String intentType) {
  293 + String prefix = "";
  294 + String suffix = "";
  295 +
  296 + if (intentType == MediaStore.ACTION_IMAGE_CAPTURE) {
  297 + prefix = "image-";
  298 + suffix = ".jpg";
  299 + } else if (intentType == MediaStore.ACTION_VIDEO_CAPTURE) {
  300 + prefix = "video-";
  301 + suffix = ".mp4";
  302 + }
  303 +
  304 + String packageName = getReactApplicationContext().getPackageName();
  305 + File capturedFile = null;
  306 + try {
  307 + capturedFile = createCapturedFile(prefix, suffix);
  308 + } catch (IOException e) {
  309 + Log.e("CREATE FILE", "Error occurred while creating the File", e);
  310 + e.printStackTrace();
  311 + }
  312 + return FileProvider.getUriForFile(getReactApplicationContext(), packageName+".fileprovider", capturedFile);
  313 + }
  314 +
297 315 private Boolean isArrayEmpty(String[] arr) {
298 316 // when our array returned from getAcceptTypes() has no values set from the webview
299 317 // i.e. <input type="file" />, without any "accept" attr
... ...