1
 private void sendPdf(String file) {
//        Uri uri = Uri.fromFile(new File(file));
        Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", new File(String.valueOf(file)));
        intent = new Intent(Intent.ACTION_SEND);
//        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION & Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

//        intent.putExtra(Intent.EXTRA_SUBJECT, "Transaction history");

        intent.setType("*/*");
        intent.putExtra(Intent.EXTRA_STREAM, uri); // invitation body

        Intent chooser = Intent.createChooser(intent, "Share with: ");

        List<ResolveInfo> resInfoList = getActivity().getPackageManager().queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY);

        for (ResolveInfo resolveInfo : resInfoList) {
            String packageName = resolveInfo.activityInfo.packageName;
            getActivity().grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }

        startActivity(chooser);
    }

I trying to share pdf using WhatsApp/Telegram but when I try to share the file, but it say unsupported attachment(Telegram). Can anyone help me?

EDIT: I got this issue only for Android 10. Anyone facing same problem?

3 Answers3

2

I use this piece of code for my own application.

    Intent mIntent = new Intent(Intent.ACTION_SEND);
    File mFile = new File(mPath);

    if(mFile.exists()) {

        mIntent.setType("application/pdf");
        mIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + mPath));
        mIntent.putExtra(Intent.EXTRA_SUBJECT,
                        "Sharing File...");
        mIntent.putExtra(Intent.EXTRA_TEXT, "Sharing File...");
        startActivity(Intent.createChooser(mIntent, "Share My File"));
        }
Saeed
  • 3,294
  • 5
  • 35
  • 52
0

It worked for me, try it. Good luck

File outputFile = new File(Environment.getExternalStoragePublicDirectory
    (Environment.DIRECTORY_DOWNLOADS), "path_of_file.pdf");
Uri uri = Uri.fromFile(outputFile);

Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/pdf");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setPackage("org.telegram.messenger");
activity.startActivity(share);   
Karthik
  • 59
  • 3
0

Add android:requestLegacyExternalStorage="true" at manifest file.