0

我无法在 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>

错误:此错误是当我使用“文件”路径时,java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/****/****/****/* **.pdf

当我使用“file1”路径(注释中的路径)时,它不会崩溃,但它给了我错误的目录/路径。位于 0/Android/data/***/etc。

也无法将 getUriForFile 路径更改为:保存我的 PDF 的 Environment.getExternalStorageDirectory().toString()。会给我第一个错误。

TIA

4

1 回答 1

0
<files-path 

是为了/data/data/<packagename>/files/.....

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

<external-path
于 2018-06-13T06:59:22.707 回答