android - 作为 Intent.ACTION_SEND 发送时不接受 Android PDF
问题描述
我有一个试图通过 Intent.ACTION_SEND 共享的 PDF 文件。虽然当我点击其中一个选项(gmail、google drive 等)时。它说“请求不包含数据”。**下面的更新代码
case R.id.share_item:
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
shareIntent.putExtra(Intent.EXTRA_STREAM, displayedFile.getAbsolutePath());
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share PDF using.."));
return true;
}
我认为问题一定出在我的 putExtra 上?话虽如此,我的文件的 URL 肯定有效,因为我可以成功地使用该路径打印 PDF。
编辑:我已经稍微更新了我的代码。
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
Uri uri = Uri.parse(displayedFile.getAbsolutePath());
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share PDF using.."));
这让我从 Whatsapp 获得“不接受文件格式”和从 Gmail 获得“无法附加文件”。
想知道问题是从内部存储调用还是不使用 FileProvider。
编辑 2:尝试添加此代码,但我遇到了崩溃
Uri outputPdfUri = FileProvider.getUriForFile(this,
PdfViewActivity.this.getPackageName() + ".provider", sharingFile);
我也将此添加到我的 Manifest 和 file_paths
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.androidextensiontest.files"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
解决方案
好吧,分享我自己的答案,因为我花了几个小时在这上面。
case R.id.share_item:
//these policy guidlines do not follow google Guidelines, recommended to change system
//https://stackoverflow.com/questions/48117511/exposed-beyond-app-through-clipdata-item-geturi
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
File sharingFile = new File(displayedFile.getPath());
Uri outputPdfUri = FileProvider.getUriForFile(this, PdfViewActivity.this.getPackageName() + ".provider", displayedFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, outputPdfUri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//Write Permission might not be necessary
shareIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share PDF using.."));
return true;
}
这个 share_item 按钮从我在内部存储中保存的文件中提取。他们的关键是正确编写清单和 xml 文件。
确保这在您的 AndroidManifest.xml 中的应用程序标签中 - 以及注意我正在使用 Androidx,相应地更改该行。我也从来没有弄清楚 android:authorities 行,但我发现填写的答案是 Github 上最常见的。
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
最后 file_paths.xml - 在您的红色文件夹中创建一个 xml 包,然后发布此代码。出于某种原因,其他代码使我的文件共享不起作用。
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="projection" path="." />
</paths>
推荐阅读
- windows - Jenkins Windows 10:没有工作树就无法使用 git-submodule
- flutter - 回复:MapBox 包 Flutter:初始化地图时我们应该如何等待/知道“onStyleLoadedCallback”是否完成?
- javascript - 如何在单词之间放置条件空格?
- pytorch - 训练时每个通道预期超过 1 个值,得到输入大小 torch.Size([1, **])
- next.js - AWS Amplify - 基于容器的托管没有选项
- javascript - 基于 JS 的 pdf 报告生成器(带有逻辑和富文本)
- node.js - https www 到非 www (NGINX)
- spring - Spring Retryable 不适用于传播=必需
- date - 如何计算pyspark函数内不同日期范围的id?
- android - 我有办法从服务中获取图像资源吗?