在Android Nougat中打开时显示空白屏幕的PDF文件

Sab*_*chi 10 android android-fileprovider android-pdf-api

我正在创建一个PDF文件并将其保存在本地存储中.当试图打开它时,它在除了Android N之外的所有设备中都是完美的.我可以使用FileProvider在Android N中打开PDF文件,但它显示为空白.

这是我的URI

content://com.products.provider/external_storage_root/Android/data/com.products/in_17052017_170502_1_1001.pdf
Run Code Online (Sandbox Code Playgroud)

这是我的代码

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();
        }
Run Code Online (Sandbox Code Playgroud)

Grz*_*zak 39

问题在于如何在intent上设置标志

pdfIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Run Code Online (Sandbox Code Playgroud)

相反,试试这个:

pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Run Code Online (Sandbox Code Playgroud)

  • 哦,我的上帝。我对此感到非常恼火。解决了。 (2认同)

And*_* P. 6

Grzegorz Matyszczak的解决方案适用于我的案例.我和Sabya Sachi的设置非常相似.更具体地说,这一行允许我看到PDF文件(来自FileProvider.getURIForFile调用的URI):

pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Run Code Online (Sandbox Code Playgroud)

关于更多信息grantUriPermissions 这里,下android:grantUriPermissions节.


Aks*_*hay 4

据我所知,你已经用过FileProvider牛轧糖。您必须在 AndroidManifest.xml 中添加 FileProvider 标记。

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.mydomain.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
</provider>
Run Code Online (Sandbox Code Playgroud)

FileProvider 只能为您事先指定的目录中的文件生成内容 URI。要将此文件链接到 FileProvider,请添加一个元素作为定义 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>
Run Code Online (Sandbox Code Playgroud)

您必须为包含您想要其内容 URI 的文件的每个目录指定 的子元素。您可以将它们添加到名为res/xml/file_paths.xml. 例如,这些 XML 元素指定两个目录:

<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>
Run Code Online (Sandbox Code Playgroud)

->你必须设置你的PDF目录路径file_paths.xml

有关更多详细信息,请参阅