First of all, I already checked out this similar question but it didn't seem to work well with my code. So here it is, my problem is that my app crashes anytime I try to open the camera in order to take a picture and store it. Here's the code I'm using:
@RequiresApi(api = Build.VERSION_CODES.M)
private void dispatchTakePictureIntent() throws IOException {
Log.i("Debug","dispatchTakePictureIntent entered");
checkCameraPermissions();
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Makes sure there's an activity that handles the intent
if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) {
File image = null;
try{
image = createImageFile();
} catch (IOException ex) {
Log.i("Debug", "IOException: " + ex);
}
// Continues if the file is created correctly
if(image != null){
Uri imageUri = Uri.fromFile(image); // <-- Here's the error
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
Log.i("Debug", "Photo succesfully saved " + takePictureIntent);
}
}
}
And also, my method createImageFile:
private File createImageFile() throws IOException {
File imageRoot = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), appDirectoryName);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File newFile = new File(imageRoot, imageFileName + ".jpg");
if(!imageRoot.exists())
if (!imageRoot.mkdirs())
return null;
mCurrentPhotoPath = newFile.getAbsolutePath();
return newFile;
}
And the error I'm getting when I press the camera button I have in-app:
FATAL EXCEPTION: main
Process: com.odoo.quipons, PID: 13976
android.os.FileUriExposedException: file:///data/user/0/com.odoo.myApp/files/Android/data/myApp/files/Pictures/JPEG_20171122_111843_.jpg exposed beyond app through ClipData.Item.getUri()