I'm creating a PDF file on my app and writing it to the external storage, "Download" directory. Now, when I open it through my app with an intent action.VIEW using FileProvider uri, Google PDF Viewer displays a blank screen, Adobe Acrobat cannot even open the file. Here's my code to write the file and then trying to show it. Note that I'm sending a local notification and trying to open the file through the notification.
try {
File mypath=new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),mParam1.getEmlak_id()+".pdf");
document.writeTo(new FileOutputStream(mypath));
document.close();
NotificationManager notificationManager = (NotificationManager)getContext().getSystemService(Context.NOTIFICATION_SERVICE);
File file = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),mParam1.getEmlak_id()+".pdf");
Uri pdfUri = FileProvider.getUriForFile(getContext(), getContext().getApplicationContext().getPackageName() + ".com.onur.emlakdosyasi.provider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(pdfUri, "application/pdf");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//use the flag FLAG_UPDATE_CURRENT to override any notification already there
PendingIntent contentIntent = PendingIntent.getActivity(getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(getContext())
.setContentTitle("title")
.setContentText("content")
.setSmallIcon(R.drawable.pdficon)
.setContentIntent(contentIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.build();
notificationManager.notify(10, notification);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Here's my provider on AndroidManifest.xml
<provider
android:name=".GenericFileProvider"
android:authorities="${applicationId}.com.onur.emlakdosyasi.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
And here's the provider paths:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
<external-path name="Download" path="Download/"/>
Note that I can open the saved file from the Download folder manually. I just cannot open it through the uri provided by FileProvider.
Changing "exported" field to true doesn't change anything.
Intent intent = new Intent(Intent. ActionVIEW) intent. setDataAndType(Uri. parse("CONTENTURI + FILENAME","application/pdf" try { startActivity(intent)} catch excpetion and so on.
FileProvider is a special subclass of ContentProvider that facilitates secure sharing of files associated with an app by creating a content:// Uri for a file instead of a file:/// Uri . A content URI allows you to grant read and write access using temporary access permissions.
To make FileProvider work follow these three steps: Define the FileProvider in your AndroidManifest file. Create an XML file that contains all paths that the FileProvider will share with other applications. Bundle a valid URI in the Intent and activate it.
I hade the same problem but in my case it was the line:
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
that was missing.
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