I have a file path: file:///storage/emulated/0/Android/data/rocks.vd.*****.develop/files/1Q 2017 financial results.pdf
this is code:
Intent intent = new Intent(Intent.ACTION_VIEW);
try {
String newFilePath = filePath.replaceAll("%20", " ");
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
intent.setDataAndType(Uri.parse(newFilePath), "application/pdf");
} else {
Uri uri = Uri.parse(newFilePath);
File file = new File(uri.getPath());
if (file.exists()){
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
context.startActivity(intent);
My uri = content://rocks.vd.*****.develop.provider/files/Android/data/rocks.vd.*****.develop/files/1Q%202017%20financial%20results.pdf
Pdf reader is opening with Unexpected error. Whats wrong?
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Here you are wiping out all your existing flags, notably your FLAG_GRANT_READ_URI_PERMISSION
. As a result, the activity that you start will not have access to your content.
Switch from setFlags()
to addFlags()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With