Skip to content

Commit

Permalink
fix(android): make camera work on Android 10 (#2559)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Mar 11, 2020
1 parent 4f6ca98 commit 4a1a7b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 4a1a7b8

Please sign in to comment.