Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a PDF file using FileProvider uri opens a blank screen

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.

like image 792
Onur Yıldırım Avatar asked Dec 30 '17 16:12

Onur Yıldırım


People also ask

How do I open a PDF in URI?

Intent intent = new Intent(Intent. ActionVIEW) intent. setDataAndType(Uri. parse("CONTENTURI + FILENAME","application/pdf" try { startActivity(intent)} catch excpetion and so on.

What is FileProvider?

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.

How do I use FileProvider?

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.


1 Answers

I hade the same problem but in my case it was the line:

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

that was missing.

like image 165
Anders Avatar answered Sep 21 '22 19:09

Anders