I'm trying to take a picture with camera, but I'm getting the following error:
FATAL EXCEPTION: main
Process: com.example.marek.myapplication, PID: 6747
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.marek.myapplication/files/Pictures/JPEG_20170228_175633_470124220.jpg
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
at com.example.marek.myapplication.MainActivity.dispatchTakePictureIntent(MainActivity.java:56)
at com.example.marek.myapplication.MainActivity.access$100(MainActivity.java:22)
at com.example.marek.myapplication.MainActivity$1.onClick(MainActivity.java:35)
AndroidManifest.xml:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.marek.myapplication.fileprovider"
android:enabled="true"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Java:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
Toast.makeText(getApplicationContext(), "Error while saving picture.", Toast.LENGTH_LONG).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.marek.myapplication.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="my_images" path="images/"/>
</paths>
I was searching whole day about this error and trying to understand FileProvider
, but I have no idea what this error message tries to tell me. If you want more info/code, write me in the comment.
What I did to solve this -
AndroidManifest.xml
filepaths.xml (Allowing the FileProvider to share all the files that are inside the app's external files directory)
and in java class -
You need to ensure that the contents of your file_paths.xml file contains this string => "/Android/data/com.example.marek.myapplication/files/Pictures/"
From the error message, that is path where your pictures are stored. See sample of expected
files_path.xml below:
none of the above worked for me, after a few hours debugging I found out that the problem is in
createImageFile()
, specificallyabsolute path
vsrelative path
I assume that you guys are using the official Android guide for taking photo. https://developer.android.com/training/camera/photobasics
Take note of the
storageDir
, this is the location where the file will be created. So in order to get the absolute path of this file, I simply useimage.getAbsolutePath()
, this path will be used inonActivityResult
if you need the Bitmap image after taking photobelow is the
file_path.xml
, simply use.
so that it uses the absolute pathand if you need the bitmap after taking photo
None of this worked for me. The only approach that works is not to declare an explicit path in xml. So do this and be happy:
Here too has a excelent tutorial about this question: https://www.youtube.com/watch?v=9ZxRTKvtfnY&t=613s
My Problem Was Wrong File Name: I Create
file_paths.xml
under res/xml while resource was set toprovider_paths.xml
in Manifest:I changed
provider_paths
tofile_paths
and problem Solved.I noticed that the policy or behaviour regarding the
path.xml
file changed between Support Library 26 and 27. For capturing a picture from the camera, I saw the following changes:With 26, I had to use
<external-path>
and the full path given in thepath
argument.With 27, I had to use
<external-files-path>
and only the subfolder in thepath
argument.So what specifically worked for me with Support Library 27 as the
path.xml
file was