I'm developing an app which requires the user to take a Picture. I create a file for saving the image. So I call the Intent like this:
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Toast.makeText(FormActivity.this, "No se pudo crear el archivo", Toast.LENGTH_SHORT).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, ACTION_IMAGE_CAPTURE);
}
}
And then, I want to read the file. With something like this
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inDither = true;
b = BitmapFactory.decodeFile(new File(mCurrentPhotoPath).getAbsolutePath(), options);
This works nice in Android 4 and 5. However, in Android 6 the Bitmap is null. I think this happens because when ActivityResult is called, the File has not been written yet. I say this because the bitmap loading code is called onResume
. And if I lock the device and unlock it again, the code works.
If I add a validation for a file size greater than 0, the code is not executed in Android 6. How can I solve this?