android - 如何在 Android 6 上打开 pdf 文件

标签 android pdf android-6.0-marshmallow

我需要打开 .pdf 文件。

在 list 中

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

在代码中

file = new File("/storage/emulated/0/Download/ccAlTT_D__.pdf");
Uri uri = Uri.fromFile(file);
Intent intentOpenFile = new Intent(Intent.ACTION_VIEW);
intentOpenFile.setDataAndType(uri, "application/pdf");
intentOpenFile.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentOpenFile);

文件路径正确。 它不适用于 android 6,并且可以在以下版本上使用。 当我点击按钮时 - pdf 文件打开,但立即关闭。

在日志

12-23 14:06:53.248 18679-16520/? E/DisplayData: openFd: java.io.FileNotFoundException: Permission denied
12-23 14:06:53.248 18679-16520/? E/PdfLoader: Can't load file (doesn't open)  Display Data [PDF : ccAlTT_D__.pdf] +UriOpenable
12-23 14:06:53.381 881-896/? E/KernelCpuSpeedReader: Failed to read cpu-freq: /sys/devices/system/cpu/cpu4/cpufreq/stats/time_in_state: open failed: ENOENT (No such file or directory)

我的 list 已满

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="***">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

最佳答案

您不应将文件作为文件 URL 传递给 Intent,因为无法保证(如此处)接收应用程序有权访问该文件。

通过FileProvider传递文件中的数据内容提供商网址。

关于android - 如何在 Android 6 上打开 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34434342/

相关文章:

android - R 不能解析为变量

android - 如何将依赖项添加到 gradle.build

javascript - Highcharts 导出图表 PDF 未显示所有类别

android - 背景大小 : auto not working as expected on android 6

android ViewPager xml膨胀错误

android - Android Studio 中具有 CPU 架构的多个构建变体

java - 错误密码异常 : Bad user password

pdf - 如何检查 TrueType 字体的 cmap 表和子表?

android - 如何清除 Android Marshmallow 中的所有应用缓存数据

android - 在 Samsung Galaxy 手机上手机进入休眠模式后,WorkManager 被取消