0

I'm trying to send a PDF file from my app to Whats app, Gmail etc. I'm able to send it to Telegram with the below mentioned code. But in case of Whats app or Gmail i'm getting errors "Sharing of Content Failed", "Cannot attach file".

pdf.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String urlPdf = takeScreenshot(true);
                        Intent shareIntent = new Intent();
                        shareIntent.setAction(Intent.ACTION_SEND);
                        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(urlPdf));
                        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        shareIntent.setType(".pdf -> application/pdf");
                        startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.sendto)));
                    }
                });

I would like to know the issue in the code if any or is it due to any permission flaws? Please rectify.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • `".pdf -> application/pdf"` is not a valid MIME type. Replace it with `"application/pdf"`. Beyond that, how is `urlPdf` created? What does it point to? – CommonsWare Jul 01 '18 at 17:53
  • Changed to "application/pdf". But still not working. urlPdf - /storage/emulated/0/App_Name/Things To Do2018-07-02_12:22:46.pdf – Aravind M J Jul 01 '18 at 18:53
  • `/storage/emulated/0/App_Name/Things To Do2018-07-02_12:22:46.pdf` is not a `Uri`. It is a filesystem path. Use `FileProvider` to serve that file to third-party apps, using its `getUriForFile()` method to get the `Uri` to use in your `Intent`. Be sure to include `FLAG_GRANT_READ_URI_PERMISSION` on the `Intent` as well. See [the documentation for `FileProvider`](https://developer.android.com/reference/android/support/v4/content/FileProvider) and [documentation on `ACTION_SEND`](https://developer.android.com/training/sharing/send#send-binary-content) for more. – CommonsWare Jul 01 '18 at 19:02

0 Answers0