Harshit Harshit -4 years ago 160
Android Question

Get content URI of images stored on sdcard using File Provider

I have an Image File stored in sd card with an Absolute path as -

storage/4469-0C17/DCIM/...
and another image stored in internal storage with an absolute path -
/storage/emulated/0/


I am using FileProvider to share it with external apps.

Now, I am able to share images stored in internal-storage but for the image stored in external storage, it throws an error as -

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/4469-0C17/DCIM/....


My Manifest -

<manifest>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application>
.....
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
</manifest>


provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>


Code -

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
File imageFileToShare = new File(filePath);
Uri imageURI = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, imageURI);
share.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(share, "Share Image"));

Answer Source

and another image stored in internal storage with an absolute path

That is what the Android SDK refers to as external storage.

but for the image stored in external storage, it throws an error as - java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/4469-0C17/DCIM/....

First, that is removable storage, not external storage.

Second, FileProvider does not support removable storage.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download