From 8b5d7d7ede088b9532d064897c7f600010f8013f Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Wed, 11 Mar 2020 19:12:19 +0100 Subject: [PATCH] fix(android): make camera work on Android 10 --- .../java/com/getcapacitor/plugin/Camera.java | 20 ++++++++----------- .../plugin/camera/CameraUtils.java | 13 +++--------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/Camera.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/Camera.java index e46e11e626..fcf4648aa2 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/Camera.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/Camera.java @@ -65,6 +65,7 @@ public class Camera extends Plugin { private static final String IMAGE_PROCESS_NO_FILE_ERROR = "Unable to process image, file not found on disk"; private static final String UNABLE_TO_PROCESS_IMAGE = "Unable to process image"; private static final String IMAGE_EDIT_ERROR = "Unable to edit image"; + private static final String IMAGE_GALLERY_SAVE_ERROR = "Unable to save the image in the gallery"; private String imageFileSavePath; private Uri imageFileUri; @@ -191,14 +192,12 @@ private CameraResultType getResultType(String resultType) { public void openCamera(final PluginCall call) { if (checkCameraPermissions(call)) { - boolean saveToGallery = call.getBoolean("saveToGallery", CameraSettings.DEFAULT_SAVE_IMAGE_TO_GALLERY); - Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) { // If we will be saving the photo, send the target file along try { String appId = getAppId(); - File photoFile = CameraUtils.createImageFile(getActivity(), saveToGallery); + File photoFile = CameraUtils.createImageFile(getActivity()); imageFileSavePath = photoFile.getAbsolutePath(); // TODO: Verify provider config exists imageFileUri = FileProvider.getUriForFile(getActivity(), appId + ".fileprovider", photoFile); @@ -225,20 +224,17 @@ public void openPhotos(final PluginCall call) { public void processCameraImage(PluginCall call) { boolean saveToGallery = call.getBoolean("saveToGallery", CameraSettings.DEFAULT_SAVE_IMAGE_TO_GALLERY); - CameraResultType resultType = getResultType(call.getString("resultType")); if(imageFileSavePath == null) { call.error(IMAGE_PROCESS_NO_FILE_ERROR); return; } - if (saveToGallery) { - Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); - File f = new File(imageFileSavePath); - Uri contentUri = Uri.fromFile(f); - mediaScanIntent.setData(contentUri); - getActivity().sendBroadcast(mediaScanIntent); + try { + MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), imageFileSavePath, "", ""); + } catch (FileNotFoundException e) { + Log.e(getLogTag(), IMAGE_GALLERY_SAVE_ERROR, e); + } } - // Load the image as a Bitmap File f = new File(imageFileSavePath); BitmapFactory.Options bmOptions = new BitmapFactory.Options(); @@ -490,7 +486,7 @@ private void editImage(PluginCall call, Uri uri) { } Intent editIntent = new Intent(Intent.ACTION_EDIT); editIntent.setDataAndType(origPhotoUri, "image/*"); - File editedFile = CameraUtils.createImageFile(getActivity(), false); + File editedFile = CameraUtils.createImageFile(getActivity()); Uri editedUri = Uri.fromFile(editedFile); editIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); editIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/camera/CameraUtils.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/camera/CameraUtils.java index d1f41d4d88..ee2889df9b 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/camera/CameraUtils.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/camera/CameraUtils.java @@ -16,22 +16,15 @@ public class CameraUtils { public static Uri createImageFileUri(Activity activity, String appId) throws IOException{ - File photoFile = CameraUtils.createImageFile(activity, false); + File photoFile = CameraUtils.createImageFile(activity); return FileProvider.getUriForFile(activity, appId + ".fileprovider", photoFile); } - public static File createImageFile(Activity activity, boolean saveToGallery) throws IOException { + public static File createImageFile(Activity activity) throws IOException { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; - File storageDir; - if(saveToGallery) { - Log.d(getLogTag(), "Trying to save image to public external directory"); - storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); - storageDir.mkdirs(); - } else { - storageDir = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES); - } + File storageDir = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES); File image = File.createTempFile( imageFileName, /* prefix */