13

I am creating a PDF file and saving it in local storage. When trying to open it, it is working perfect in all devices except in Android N. I am able to open PDF file in Android N using FileProvider, but it is displaying as blank.

This is my URI

content://com.products.provider/external_storage_root/Android/data/com.products/in_17052017_170502_1_1001.pdf

This is my code

Uri path;

File pdfFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
                + "Android" + "/" + "data" + "/" + "com.products" + "/" + file);

if (Build.VERSION.SDK_INT >= 24) {
            path = FileProvider.getUriForFile(getActivity(), "com.products.provider", pdfFile);
        } else {
            path = Uri.fromFile(pdfFile);
        }

        // Setting the intent for pdf reader
        Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
        pdfIntent.setDataAndType(path, "application/pdf");
        pdfIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

        try {
            startActivity(pdfIntent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(getActivity(), "Can't read pdf file", Toast.LENGTH_SHORT).show();
        }
Sabya Sachi
  • 145
  • 2
  • 10
  • It might be in zoomed state. Did you try to zoom out and see? Are you able to see page count? – Ram May 17 '17 at 05:40
  • It isn't in zoomed state. Only file name is displaying on top, rest of the screen is blank. When opened the same file through explorer, I can see the data. – Sabya Sachi May 17 '17 at 05:53
  • May I know what app you are using to view the PDF file? and few more troubleshooting questions :) 1. Did you try to open any other PDF file? 2. Did you try any other PDF app? – Ram May 17 '17 at 06:02
  • when tried with Drive PDF Viewer and Google PDF Viwer, PDF is blank . When tried with Adobe Acrobat Reader, i got message as "This file cannot be accessed. Check the location and try again" – Sabya Sachi May 17 '17 at 06:25
  • Try to open an input stream for the uri yourself. And read from it a bit. – greenapps May 17 '17 at 07:55
  • Add a pdfFile.exists() check before you make that intent. – greenapps May 17 '17 at 07:58
  • @greenapps PdfFile exists. It is opening file but it is blank. – Sabya Sachi May 17 '17 at 09:38
  • And the input stream? Please react serious to suggestions. – greenapps May 17 '17 at 11:31
  • Problem is not only on Nougat. It lies from Lollipop 5.1 (API 22) to Nougat 7.1.1 (API 25). By the way, haven't checked this issue on Oreo. – zackygaurav Feb 06 '18 at 05:40

4 Answers4

45

The problem is with how you are setting the flags on the intent

pdfIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Instead, try this:

pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
7

Grzegorz Matyszczak's solution worked for my case. I had a very similar setup to Sabya Sachi. More specifically this line allowed me to see the PDF file (URI from FileProvider.getURIForFile call):

pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

More info on grantUriPermissions here, under android:grantUriPermissions section.

Kylo-Rey
  • 1,266
  • 10
  • 12
4

As I can see that you have used FileProvider for Nougat. You have to add a FileProvider tag in AndroidManifest.xml.

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.mydomain.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
</provider>

A FileProvider can only generate a content URI for files in directories that you specify beforehand.To link this file to the FileProvider, add a element as a child of the element that defines the FileProvider.

<provider
           android:name="android.support.v4.content.FileProvider"
           android:authorities="${applicationId}.provider"
           android:exported="false"
           android:grantUriPermissions="true">
          <meta-data
               android:name="android.support.FILE_PROVIDER_PATHS"
               android:resource="@xml/file_paths"/>
</provider>

You must specify a child element of for each directory that contains files for which you want content URIs. You can add them to a new file called res/xml/file_paths.xml. For example ,these XML elements specify two directories:

<paths  xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="my_images" path="images/"/>
    <files-path name="my_docs" path="docs/"/>
</paths>

->you have to set your PDF directory path in file_paths.xml

For more detail refer this

Akshay
  • 6,029
  • 7
  • 40
  • 59
0
  1. Replace FileProvider class with MyFileProvider class:

path = MyFileProvider.getUriForFile(getActivity(), "com.products.provider", pdfFile);

where MyFileProvider should be a generic class which extends FileProvider:

public class MyFileProvider extends FileProvider {
}
  1. Modify AndroidManifest by setting your MyFileProvider class location to android:name attribute in your provider section:
android:name=".utils.MyFileProvider"
Andrii Artamonov
  • 622
  • 8
  • 15