13

我正在创建一个 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

这是我的代码

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();
        }
4

4 回答 4

45

问题在于您如何在意图上设置标志

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

相反,试试这个:

pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
于 2017-06-07T10:26:13.247 回答
7

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

pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

更多信息在grantUriPermissions 这里,在android:grantUriPermissions部分下。

于 2017-07-13T16:06:50.710 回答
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>

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>

您必须为每个包含您想要内容 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>

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

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

于 2017-05-17T06:13:59.663 回答
0
  1. 用 MyFileProvider 类替换 FileProvider 类:

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

whereMyFileProvider应该是一个扩展的泛型类FileProvider

public class MyFileProvider extends FileProvider {
}
  1. 通过将您的 MyFileProvider 类位置设置为android:name您的部分中的属性来修改 AndroidManifest provider
android:name=".utils.MyFileProvider"
于 2019-08-12T16:26:40.260 回答