C. Skjerdal
C. Skjerdal

Reputation: 3108

Android PDF not accepted when sending as Intent.ACTION_SEND

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>

Upvotes: 2

Views: 3088

Answers (1)

C. Skjerdal
C. Skjerdal

Reputation: 3108

Alright sharing my own answer because I spent hours on this.

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

This share_item button pulls from my saved file in Internal Storage. They key was getting the manifest and xml file written correctly.

Make sure this goes within your application tags in your AndroidManifest.xml - As well as notice I am using Androidx, change that line accordingly. I also never figured out the android:authorities line, but I found the filled in answer was the most common on 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>

Lastly file_paths.xml - create an xml package inside your red folder, and post this code. For some reason other code was what made my file sharing not work.

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

Upvotes: 4

Related Questions