PDF file when opening in Android Nougat is showing blank screen

廉价感情. 提交于 2019-12-03 12:12:27

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);

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.

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

  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"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!