Reputation: 7166
For the past couple of hours I've been busy getting the FileProvider to work to open files in other apps. Somewhere in the process something is going wrong because apps just don't have the permission to open the files I put in a Intent. I really hope someone can help me with this issue.
My knowledge of android programming is not great. So it might just be a really dumb mistake
Here is my code.
To open a file I do this (fileToOpen length is around 300kb so the file is loaded in the variable):
File fileToOpen = new File(Environment.getExternalStorageDirectory(), path);
Uri contentUri = FileProvider.getUriForFile(this, "me.albie.share", fileToOpen);
Intent openIntent = new Intent(Intent.ACTION_VIEW);
openIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
openIntent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
openIntent.setDataAndType(contentUri, "application/pdf");
openIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
PackageManager pm = this.getPackageManager();
if (openIntent.resolveActivity(pm) != null) {
this.startActivity(Intent.createChooser(openIntent, "Open file"));
}
Provider in manifest file:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="me.albie.share"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Xml file with the path
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="/"/>
</paths>
Upvotes: 2
Views: 888
Reputation: 1006614
openIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
openIntent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
openIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
setFlags()
replaces all previous flags, so your FLAG_ACTIVITY_NO_HISTORY
wipes out your two permission flags. Either:
Switch each of these to addFlags()
, or
Use a single setFlags()
call:
openIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
Intent.FLAG_ACTIVITY_NO_HISTORY);
Upvotes: 2