首页 > 解决方案 > 作为 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>

标签: androidpdfshare-intent

解决方案


好吧,分享我自己的答案,因为我花了几个小时在这上面。

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>

推荐阅读