【问题标题】:How to create content provider for sharing PDF file through implicit intent to another app?如何通过隐式意图创建内容提供程序以将 PDF 文件共享到另一个应用程序?
【发布时间】:2020-03-24 04:06:15
【问题描述】:

我正在为我的学校制作项目。在这个应用程序中,我从 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 文件。如果文件路径有任何更正,请建议我。

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

帮帮我! Emulator Image

【问题讨论】:

  • 您从未知变量“localFile”开始。我们应该知道那是什么吗?请开始更好。
  • getUriForFile() 是你自己的函数。我们不知道它的作用。
  • 进一步你应该告诉我们 fileUri.toString() 的值。
  • not sure, if i declared file_path correct. ?您没有在代码中声明文件路径。如前所述:从它开始。
  • No getUriForFile() 不是我自己的函数。我接受了从 FileProvider.getUriForFIle() @blackapps 导入

标签: java android pdf fileprovider


【解决方案1】:

android:authorities="com.syrous.yccestudentlibraryjava.fileprovider 不匹配 content://com.syrous.studentlibraryjava.fileprovider/document/mse1_xfaefga.pdf

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 1970-01-01
    相关资源
    最近更新 更多