首页 > 解决方案 > 如何通过隐式意图创建内容提供程序以将 PDF 文件共享到另一个应用程序?

问题描述

我正在为我的学校制作项目。在这个应用程序中,我从 Firebase 下载 pdf 文件并将其存储在应用程序的外部目录中。下载后,我想通过隐式意图到其他pdf阅读器应用程序来显示下载的pdf。我直接在网上搜索了 2 天以了解如何创建内容提供程序,但所有教程都是旧的并且使用了 android.support.v4 内容提供程序,我不知道如何创建自定义内容提供程序。我尝试使用 androidx.core.content.fileprovider 并且它还创建了内容 uri,但是在启动时,会弹出 pdf 阅读器屏幕但没有显示任何内容,只显示文件的标题。

fileUri.toString-content://com.syrous.studentlibraryjava.fileprovider/document/mse1_dagaeg.pdf 的输出

    Uri fileUri = getUriForFile(getApplicationContext(), getPackageName()+".fileprovider", localFile); //This is the in build function, just accepted import. 
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setDataAndType(fileUri, "application/pdf");
    i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);

这是manifest中provider的声明---->

 <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.syrous.yccestudentlibraryjava.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"/>
    </provider>

这是file_paths ----->

  <paths>
    <external-files-path
        name="document"
        path="mse1/"/>
  </paths>

我不确定,如果我声明 file_path 正确。我在 external_folder/files/mse1/file_to_open 中有我的 pdf 文件。如果 file_path 有任何更正,请建议我。

我有权读取和写入清单中声明的​​外部存储,并且还要求它

帮帮我吧伙计们!! 模拟器图像

标签: javaandroidpdffileprovider

解决方案


android:authorities="com.syrous.yccestudentlibraryjava.fileprovider does not mach content://com.syrous.studentlibraryjava.fileprovider/document/mse1_xfaefga.pdf.


推荐阅读