17

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.

Onur Yıldırım
  • 355
  • 1
  • 2
  • 10

4 Answers4

11

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

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

that was missing.

Anders
  • 288
  • 4
  • 10
9
  File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Ap/" + "manual.pdf");  // -> filename = manual.pdf

    Uri excelPath;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

        excelPath = FileProvider.getUriForFile(mContext, "YourPackageName", pdfFile);
    } else {
        excelPath = Uri.fromFile(pdfFile);
    }
    Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
            pdfIntent.setDataAndType(excelPath, "application/pdf");
            pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            pdfIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            try{
        startActivity(pdfIntent);
    }catch(ActivityNotFoundException e){
        Toast.makeText(mContext, "No Application available to view PDF", Toast.LENGTH_SHORT).show();
    }
Triyugi Narayan Mani
  • 3,039
  • 8
  • 36
  • 56
Ap11
  • 91
  • 1
  • 1
  • With the help of this code you can open pdf file in any android device – Ap11 Jan 17 '19 at 05:27
  • I'm trying to decide on the same issue. When calling of getUriForFile is happened then next exception occurs Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData...' I have replaced getApplicationContext().getPackageName() to YourPackageName. File exists and I'm checking that. Permissions have granted. I'm using AndroidX, is it possible that it is my pain? android:name="androidx.core.content.FileProvider Do you have any ideas? Android 8.0 – Orlov Const Nov 15 '19 at 07:54
  • I have solved the issue. You have to replace YourPackageName to what placed in android:authorities for example "com.example.android.fileprovider". My package was named as com.example.root.photolist but to write its name will be incorrect. It doesn't work if I point to this name. You may read there in details https://forums.bignerdranch.com/t/solved-error-at-taking-picture-with-intents-need-serious-help/13900 – Orlov Const Nov 15 '19 at 08:09
5

Tried everything for hours and posted here for last resort. After 15 mins, solved it..

For anyone wondering, I changed name attribute of provider from

android:name=".GenericFileProvider"

to

android:name="android.support.v4.content.FileProvider"

I don't even know why I have created a file provider class myself but I remember following a tutorial for it.

Onur Yıldırım
  • 355
  • 1
  • 2
  • 10
0

For my case adding -

/* If set, the new activity is not kept in the history stack.  
 * As soon as the user navigates away from it, the activity is finished. */

 intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

was worked !!!

Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42