PDF file when opening in Android Nougat is showing blank screen
Asked Answered
W

4

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();
        }
Waterer answered 17/5, 2017 at 5:17 Comment(9)
It might be in zoomed state. Did you try to zoom out and see? Are you able to see page count?Tatary
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.Waterer
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?Tatary
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"Waterer
Try to open an input stream for the uri yourself. And read from it a bit.Armandinaarmando
Add a pdfFile.exists() check before you make that intent.Armandinaarmando
@Armandinaarmando PdfFile exists. It is opening file but it is blank.Waterer
And the input stream? Please react serious to suggestions.Armandinaarmando
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.Disbar
M
46

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);
Matrona answered 7/6, 2017 at 10:26 Comment(3)
This was it for me! Thanks.Corotto
OH.MY.GOD. I was getting really annoyed with this. Solved.Masera
YOU DA MAN!!! several days wasted with this issue. Your solution workedSupervisor
D
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.

Declan answered 13/7, 2017 at 16:6 Comment(0)
B
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

Boling answered 17/5, 2017 at 6:13 Comment(6)
<paths> <external-path name="external_files" path="." /> </paths> But this is also not workingWaterer
No, I am not getting anything in logWaterer
what is your targetSdkVersion?Boling
targetSdkVersion 25, I can't decrease it as I am using some library which is not compatible with 23Waterer
try to Clean your project once.Boling
I have tried everything, but nothing works. Is there any other solution for this?Waterer
S
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"
Schenck answered 12/8, 2019 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.