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.
If nothing helps and you are getting the error
then try changing some line like:
to:
and in the xml file:
which is weird, since the folder I access is
/images
.The following change in xml file_paths file worked for me.
I see that at least you're not providing the same path as others in file_paths.xml. So please make sure you provide the exact the same package name or path in 3 places including:
android:authorities
attribute in manifestpath
attribute in file_paths.xmlauthority
argument when calling FileProvider.getUriForFile().Lost 2 weeks trying to find some solution... If you arrive here after try everthing above:
1 - Verify if your tag provider are inside tag application
2 - If you try a lot paths way without success, then test with this:
Test this, if camera works, then start eliminate some lines and continue testing...
3 - No forget verify if camera are actived in your emulator.
I have spent 5 hours for this..
I have tried all the methods above but it depends on the what storeage your app currently using.
https://developer.android.com/reference/android/support/v4/content/FileProvider#GetUri
Check the documentation before trying the codes.
In my case since
files-path
sub directory will beContext.getFilesDir()
. The funky thing is itContext.getFilesDir()
notes one another subdirectory.what I am looking for is
data/user/0/com.psh.mTest/app_imageDir/20181202101432629.png
returns
/data/user/0/com.psh.mTest/files
so the tag should be
....
files-path name="app_imageDir" path="../app_imageDir/" ......
Then it works!!
Your file is stored under
getExternalFilesDir()
. That maps to<external-files-path>
, not<files-path>
. Also, your file path does not containimages/
in it, so thepath
attribute in your XML is invalid.Replace
res/xml/file_paths.xml
with: