Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): make camera work on Android 10 #2559

Merged
merged 1 commit into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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