作为 Intent.ACTION_SEND 发送时 Android PDF 不被接受

标签 android pdf share-intent

我有一个 PDF 文件,正在尝试通过 Intent.ACTION_SEND 共享。尽管当我点击其中一个选项(gmail、googledrive等)时。它说“请求不包含数据”。 **更新了下面的代码

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 按钮从我在内部存储中保存的文件中提取。关键是正确编写 list 和 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>

关于作为 Intent.ACTION_SEND 发送时 Android PDF 不被接受,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55978638/

相关文章:

c# - 运行组合框中显示的 .pdf 最简单的方法是什么?

android - 如何将 Android 中的大型 pdf 文件加载到我的 webview 中

android - 有没有办法获取android "history"系统日志?

java - 回家后应用程序不会存储状态

c# - iText7 - 将 HTML 转换为 PDF 时不呈现 CSS3 文本溢出属性

android - Intent 共享无法附加图像

android - 如何以编程方式将图像或视频发送到android中的whatsapp状态?

java - 9-patch 文件中出现奇怪的黑边 - android

android - 具有起始偏移量的 RecyclerView