打开PDF牛轧糖+(文件目录)

时间:2018-06-05 02:45:53

标签: android android-7.1-nougat androidpdfviewer

我无法在Ver上打开PDF。 Nougat +,以下代码后出错。

以任何其他方式或更好的方式打开pdf,或者如何更改外部目录的路径。

以下代码:

        Intent intent = new Intent(Intent.ACTION_VIEW);
        try {
            Uri path = Uri.fromFile(files.get(position));

            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                intent.setDataAndType(path, "application/pdf");
            } else {
                Uri uri = Uri.parse(String.valueOf(files.get(position)));
                File file = new File(uri.getPath());
                if (file.exists()) {
                    //File file1 = new File(getActivity().getFilesDir() + File.separator + "PDF" + File.separator + "eFSIS" + File.separator + "InspectionOrder.pdf");
                    Toast.makeText(mainActivity, file.toString(), Toast.LENGTH_SHORT).show();
                    uri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", file);
                    intent.setDataAndType(uri, "application/pdf");
                    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                }
            }
            intent = Intent.createChooser(intent, "Open With");
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            getActivity().startActivity(intent);
        } catch (ActivityNotFoundException e) { }

提供者: (清单)

<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/provider_paths"/>
        </provider>

的xml:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="internal_files" path="."/>
</paths>

错误: 当我使用&#39;文件时,会出现此错误。路径,  java.lang.IllegalArgumentException:无法找到包含/ storage / emulated / 0 / **** / **** / **** / ***的已配置根目录.pdf

当我使用&#39; file1&#39;路径(评论中的那个),它没有崩溃,但它给了我错误的目录/路径。这是0 / Android / data / *** /等。

还可以将getUriForFile路径更改为:Environment.getExternalStorageDirectory()。toString(),其中保存了我的PDF。 会给我第一个错误。

TIA

1 个答案:

答案 0 :(得分:0)

<files-path 

适用于/data/data/<packagename>/files/.....

/storage/emulated/0/.....更改为

<external-path
相关问题