Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t打开用于android的pdf文件_Android_Android Intent - Fatal编程技术网

Can';t打开用于android的pdf文件

Can';t打开用于android的pdf文件,android,android-intent,Android,Android Intent,我彻底检查了pdf文件是否正确 在里面 /storage/emulated/0/Download/Abcd.pdf“ 但不能故意打开它 我在不同的浏览器中打开了它,一些浏览器导致了一个错误:“无法打开文件”。Microsoft word说:检查设备中的文件,但当我在android文件系统的文件目录中打开它时,Abcd.pdf文件打开得很好 我把路线定错了吗 AndroidManifest.xml <provider android:authorities="${appli

我彻底检查了pdf文件是否正确 在里面
/storage/emulated/0/Download/Abcd.pdf“
但不能故意打开它

我在不同的浏览器中打开了它,一些浏览器导致了一个错误:“无法打开文件”。Microsoft word说:
检查设备中的文件
,但当我在android文件系统的文件目录中打开它时,
Abcd.pdf
文件打开得很好

我把路线定错了吗

AndroidManifest.xml

<provider
        android:authorities="${applicationId}.provider"
        android:name="android.support.v4.content.FileProvider"
        android:exported="false"
        android:grantUriPermissions="true">

        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>
res/xml/provider_path.xml

<?xml version="1.0" encoding="utf-8"?>
 <paths xmlns:android="http://schemas.android.com/apk/res/android">
 <external-path
name="storage/emulated/0"
path="."/>
</paths>

我正在使用此功能:

public void openPdf(LocalFile magazine) {

        if (BuildConfig.DEBUG)
            Log.d(TAG, "openPdf() called with: magazine = [" + magazine + "]");

        Intent intent = new Intent(Intent.ACTION_VIEW);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

            File file = new File(Uri.parse(magazine.getPath()).getPath());
            Uri uri = FileProvider.getUriForFile(getContext(),
                getContext().getApplicationContext().getPackageName() + ".provider", file);
            intent.setDataAndType(uri, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        } else {
            intent.setDataAndType(Uri.parse(magazine.getPath()), "application/pdf");
        }

        try {
            startActivity(intent);
        } catch (Throwable t) {
            t.printStackTrace();
            //attemptInstallViewer();
        }

    }

这对我来说很好。

我有一个问题可以解决这个问题,您是否在清单中写入了存储权限

您的应用程序是否具有读取外部存储的权限?请从移动设置中检查此项,它将自动开始工作

下面是如何在清单文件中初始化权限语句

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

如果在清单中给出,并且您的手机已保证权限,且应用程序无法运行,那么请共享完整的活动代码,我可能会在这方面为您提供帮助。

intent.setFlags(intent.FLAG\u GRANT\u READ\u URI\u PERMISSION);保存我。谢谢
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />