I have a PDF file I am trying to share through Intent.ACTION_SEND. Though when I click on one of the options (gmail, google drive, etc). It says "Request contains no data." **Updated Code Below
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;
}
I assume the issue must be with my putExtra? That being said the URL for my file definitely works because I can successfully use that path for printing PDFs.
Edit: I've Updated my code a smidge.
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.."));
This gets me "file format not accepted" from Whatsapp and "Unable to attach file" from Gmail.
Wondering if the issue is with calling from Internal Storage or not using FileProvider.
Edit 2: Trying to add this code but I am getting a crash
Uri outputPdfUri = FileProvider.getUriForFile(this,
PdfViewActivity.this.getPackageName() + ".provider", sharingFile);
I've also added this to my Manifest and 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>