user5629998
user5629998

Reputation:

FileProvider: Failed to find configured root

I have been trying to share a pdf file, I set up the FileProvider like this:

On the main manifest xml:

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

The res/xml/file_paths.xml file:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path name="my_files" path="." />
</paths>

And in my code I am trying the following:

String path = root.getAbsolutePath() + "/file.pdf";
final Uri data = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID+".fileprovider", new File(path));
getApplicationContext().grantUriPermission(getApplicationContext().getPackageName(), data, Intent.FLAG_GRANT_READ_URI_PERMISSION);
final Intent intent = new Intent(Intent.ACTION_VIEW).setDataAndType(data, "application/pdf").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
getApplicationContext().startActivity(intent);

Returns error:

     Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/file.pdf

Upvotes: 1

Views: 8978

Answers (2)

RJnr
RJnr

Reputation: 866

The accepted answer is correct, but he didn't specify the API to use, so here it is.

 val imagePath = File(
    Environment.getExternalStorageDirectory().path +
            File.separator, "Your folder name"
)
val newfile = File(imagePath, "Your file name")


val imageUri = FileProvider.getUriForFile(
    context,
    "com.example.domain.provider",
    newfile
)
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "image/jpg" // set file type 
shareIntent.putExtra(
    Intent.EXTRA_STREAM,
    imageUri
)
context.startActivity(Intent.createChooser(shareIntent, "Share Status Saver Image"))

Happy Coding!

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007409

The documentation for FileProvider says that <files-path>:

Represents files in the files/ subdirectory of your app's internal storage area. This subdirectory is the same as the value returned by Context.getFilesDir().

Your file is not in internal storage. It is in external storage. For that, you need an <external-path> element, not a <files-path> element.

Upvotes: 5

Related Questions