[ACCEPTED]-Open file in cache directory with ACTION_VIEW-android-intent
Accepted answer
I found out that I had to do things differently.
Instead 11 of creating my own ContentProvider, the 10 v4 support library offers a FileProvider 9 class that can be used.
In AndroidManifest.xml
add
<application ...>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="be.myapplication"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
The FILE_PROVIDER_PATHS 8 is an xml file that describes which files 7 can be read by other applications.
So I added 6 a file_paths.xml file to the res/xml folder.
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="my_cache" path="." />
</paths>
And 5 the only thing you have to do then is create 4 and start the intent to show the file.
File file = new File(getCacheDir(), "test.pdf");
Uri uri = FileProvider.getUriForFile(context, "be.myapplication", file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
And 3 actually that's it.
IMPORTANT: Please do not forget 2 setting
FLAG_GRANT_READ_URI_PERMISSION
flags for the intent. Otherwise 1 it will not work.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.