Some tweaks to Bhoomika's answer worked for me
`xml/providers_path.xml`
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="files" path="."/>
</paths>
`AndroidManifest.xml`
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="APPLICATION_ID.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
A Kotlin Extension function to open the PDF from an activity
fun Activity.openPdf(filename: String?) {
val file = File(filesDir, filename)
if(file.exists()) {
Intent(Intent.ACTION_VIEW).apply {
setDataAndType(FileProvider.getUriForFile(this@openPdf, BuildConfig.APPLICATION_ID + ".provider", file), "application/pdf")
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
try {
startActivity(this)
} catch (e: Exception) {
showErrorDialog(unable_to_open_doc)
}
}
} else {
showErrorDialog(file_doesnt_exist)
}
}
If your `targetSdkVersion >= 24`, then we have to use `FileProvider` class to give access to the particular file or folder to make them accessible for other apps. We create our own class inheriting `FileProvider` in order to make sure our FileProvider doesn't conflict with FileProviders declared in imported dependencies as described [here][2].
Steps to replace `file://` URI with `content://` URI:
- Add a class extending `FileProvider`
public class GenericFileProvider extends FileProvider {}
- Add a FileProvider `<provider>` tag in `AndroidManifest.xml` under `<application>` tag. Specify a unique authority for the `android:authorities` attribute to avoid conflicts, imported dependencies might specify `${applicationId}.provider` and other commonly used authorities.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-xml -->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<application
...
<provider
android:name=".GenericFileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
</manifest>
<!-- end snippet -->
- Then create a `provider_paths.xml` file in `res/xml` folder. Folder may be needed to created if it doesn't exist. The content of the file is shown below. It describes that we would like to share access to the External Storage at root folder `(path=".")` with the name **external_files**.
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-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>
<!-- end snippet -->
- The final step is to change the line of code below in
Uri photoURI = Uri.fromFile(createImageFile());
to
Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".my.package.name.provider", createImageFile());
- **Edit:** If you're using an intent to make the system open your file, you may need to add the following line of code:
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Please refer, full code and solution has been explained [here.][1]
[1]: https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en
[2]: https://commonsware.com/blog/2017/06/27/fileprovider-libraries.html