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.
Your file is stored under getExternalFilesDir()
. That maps to <external-files-path>
, not <files-path>
. Also, your file path does not contain images/
in it, so the path
attribute in your XML is invalid.
Replace res/xml/file_paths.xml
with:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path name="my_images" path="/" />
</paths>
UPDATE 2020 MAR 13
Provider path for a specific path as followings:
<files-path/>
--> Context.getFilesDir()
<cache-path/>
--> Context.getCacheDir()
<external-path/>
--> Environment.getExternalStorageDirectory()
<external-files-path/>
--> Context.getExternalFilesDir(String)
<external-cache-path/>
--> Context.getExternalCacheDir()
<external-media-path/>
--> Context.getExternalMediaDirs()
Ref: https://developer.android.com/reference/androidx/core/content/FileProvider
This may resolve everyones problem: All tags are added so you don't need to worry about folders path. Replace res/xml/file_paths.xml with:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</paths>
EDIT: 1st June 2021
We should use only specific path which we need. Try alternate path on your own and use which you needed.
See Accepted answer for more information: https://stackoverflow.com/a/42516202/4498813
Got similar problem after enabled flavors (dev, stage).
Before flavors my path resource looked like this:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="my_images"
path="Android/data/pl.myapp/files/Pictures" />
</paths>
After added android:authorities="${applicationId}.fileprovider" in Manifest appId was pl.myapp.dev or pl.myapp.stage depends on flavor and app started crashing. I removed full path and replaced it with dot and everything started working.
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="my_images"
path="." />
</paths>
I am sure I am late to the party but below worked for me.
<paths>
<root-path name="root" path="." />
</paths>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With