【问题标题】:Android Sharing a PDF File to another app under SDK 27Android 在 SDK 27 下将 PDF 文件共享到另一个应用程序
【发布时间】:2018-08-06 14:45:26
【问题描述】:

最近升级到 SDK 27,现在我的文件共享功能坏了。这个想法是让用户点击“共享文件”按钮,Android 会向他们显示可用的应用程序,然后他们选择一个将选定的 PDF 文件附加到该应用程序。例如,如果他们选择 GMail,文件将被附加,他们将输入主题、“收件人”地址并提交。

最初的问题是 uri.parse 方法已被弃用(我假设是出于安全原因),现在已被 FileProvider.getUriForFile 取代,该方法适用于应用程序中的其他场景(加载和保存媒体文件),但在此case GMail 启动但附件永远不会被附加。不会抛出任何错误。

在 SDK 28 下仍然可以通过这种方式附加文件吗?

代码如下:

Intent shareIntent = new Intent(Intent.ACTION_SEND);
File output = new File(pdfFilePath);
Uri uri = FileProvider.getUriForFile(
    MyActivity.this,
    MyActivity.this.getApplicationContext().getPackageName() + ".provider", output);
shareIntent.setType("application/pdf");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(shareIntent);

我已经验证文件存在,路径是:

 /storage/emulated/0/myapp/8d7a79dd-2c23-49c3-bd36-68335d418268.pdf

生成的 URI 不为空,在调试器的 uriString 中有如下内容:

content://com.mycompany.myapp.provider/external_files/myapp/8d7a79dd-2c23-49c3-bd36-68335d418268.pdf

Android 是否期待其他模式?在运行 Android 8 的三星 Galaxy Tab S3 上进行测试。

【问题讨论】:

  • 你的清单中有 FileProvider 的配置文件吗?

标签: android pdf android-intent


【解决方案1】:

突然之间,这开始起作用了。我在想调试器或某些东西已损坏。清理、重建并重新启动 Android Studio,突然一切都好了。我将把它留在这里只是为了举例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多