【问题标题】:How to open a PDF file using a third-party application (intent, url)如何使用第三方应用程序打开 PDF 文件(intent、url)
【发布时间】:2019-10-06 12:44:42
【问题描述】:
       int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
    } else {
        File file = new File("/sdcard/reportSituationAddress.pdf");
        if (file.exists()) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider" ,file);
            intent.setDataAndType(uri, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(Pdftest.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(Pdftest.this, "not found", Toast.LENGTH_LONG).show();
        }

我有一个 pdf 文件,我需要点击一个按钮来打开它。这样用户点击后可以选择应用程序打开文件,然后文件就会在其中打开。我使用提供程序,但经常出错。请帮忙解决问题。

它是我的文件提供者

    <?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Android/data/telros.brigade/files/Pictures" />
    <external-files-path name="name" path="AisBrigade/Pdf" />
</paths>

这是我的清单

<provider
            android:name="androidx.work.impl.WorkManagerInitializer"
            android:authorities="ru.npobaltros.aisbrigade.mapsdk.storage.workmanager-init_new"
            android:enabled="false"
            android:exported="false"
            tools:replace="android:authorities" />
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="telros.brigade"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

应该是这样的,只有打开pdf的程序。

我的错误

也许有人有一段工作代码,在哪里实现,如果有,请展示

【问题讨论】:

  • 嘿,你有没有找到解决方案。遇到同样的问题,似乎无法找到关于该主题的直接答案。

标签: android pdf url provider


【解决方案1】:

FileProvider 元数据中没有涵盖所选路径的条目。

首先,停止硬编码路径。使用方法从根位置组装路径。我的猜测是您认为您的 PDF 位于 external storage 的根目录中。在这种情况下,您将使用:

File file = new File(Environment.getExternalStorageDirectory(), "reportSituationAddress.pdf");

然后,确保您的 FileProvider 元数据支持您的根位置。对于Environment.getExternalStorageDirectory()use &lt;external-path&gt;

另外,请注意you have very little filesystem access on Android 10 and higher。例如,Environment.getExternalStorageDirectory() 在 Android 10 上已弃用,因为默认情况下您无权访问它。

【讨论】:

  • 我修正了你所说的“文件”。但我的 FileProvider: schemas.android.com/apk/res/android"> 我还是有同样的错误
猜你喜欢
  • 1970-01-01
  • 2017-01-25
  • 2017-06-02
  • 1970-01-01
  • 1970-01-01
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多