1

Provider in manifest.xml

<!-- File Provider -->
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.test.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

Resource file

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

Use of fileprovider

Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "*/*");
intent.startActivity(intent);

Reference : here

Description : I can't open image, text file, pdf using this file provider. When i open the file text file its give me ERR_UNKNOWN_URL_SCHEME. If i open image or pdf its nothing show anything

Community
  • 1
  • 1
Sumit Bhatt
  • 718
  • 4
  • 19

1 Answers1

2

You need to add the "FLAG_GRANT_READ_URI_PERMISSION" flag to your intent:

Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "*/*");
intent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);
intent.startActivity(intent);

https://developer.android.com/reference/android/content/Intent.html#FLAG_GRANT_READ_URI_PERMISSION

FLAG_GRANT_READ_URI_PERMISSION If set, the recipient of this Intent will be granted permission to perform read operations on the URI in the Intent's data and any URIs specified in its ClipData. When applying to an Intent's ClipData, all URIs as well as recursive traversals through data or other ClipData in Intent items will be granted; only the grant flags of the top-level Intent are used.

Aldasa
  • 4,551
  • 2
  • 21
  • 31
  • Nice, this solved it in my case. It is weird, since I thought the `grantUriPermissions` in the manifest for the provider would be sufficient. – Peterdk May 14 '19 at 16:41