作为意图发送时不接受Android PDF。操作\u发送

作为意图发送时不接受Android PDF。操作\u发送,android,pdf,share-intent,Android,Pdf,Share Intent,我有一个PDF文件,我正试图通过Intent.ACTION\u发送共享。但当我点击gmail、谷歌硬盘等选项时,它说请求中不包含任何数据。 **更新代码如下 case R.id.share_item: final Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("application/pdf"); shareIntent.

我有一个PDF文件,我正试图通过Intent.ACTION\u发送共享。但当我点击gmail、谷歌硬盘等选项时,它说请求中不包含任何数据。 **更新代码如下

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;
    }
我想问题一定出在我的电脑上?这就是说,我的文件的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);
我还将其添加到清单和文件路径中

<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);

            Uri uri = Uri.parse("file://" + displayedFile.getAbsolutePath() + ".pdf");
            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;
    }
此共享项按钮从内部存储器中保存的文件中提取。他们的关键是正确写入清单和xml文件

确保这在您的AndroidManifest.xml中的应用程序标记中—注意我正在使用Androidx,相应地更改该行。我也从来没有想到android:Authories这一行,但我发现填写的答案是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>
最后,在红色文件夹中创建一个xml包,并发布此代码。由于某种原因,其他代码使我的文件共享无法工作

<?xml version="1.0" encoding="utf-8"?>
<paths>
  <files-path name="projection" path="." />
</paths>