Share Pdf File Via Whatsapp From My App On Android
I am try to send pdf file from my app to whatsapp, and here is the code, but something missing!! it opens whatsapp and i can choose a contact but it says 'sharing failed'! the cod
Solution 1:
I figured out the problem, and here is the answer if somebody had the same issue. The problem was that I am trying to open the pdf from the asset folder which did n't work, and if would try to open the pdf from the download folder for example, it would work. Please refer to the the code below for the final correct way:
FileoutputFile=newFile(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOWNLOADS), "ref Number from Quotation.pdf");
Uriuri= Uri.fromFile(outputFile);
Intentshare=newIntent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/pdf");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setPackage("com.whatsapp");
activity.startActivity(share);
Solution 2:
File outputPath= newFile(Environment.getExternalStorageDirectory() + "/MyPdf/example.pdf");//change with your pathIntentshareIntent=newIntent(Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
UrifileUri= FileProvider.getUriForFile(getApplicationContext(),
getPackageName() + ".fileprovider", outputPath);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
startActivity(Intent.createChooser(shareIntent, "Share it"));
Solution 3:
It's technically wrong, what if someone has WhatsApp business or want to share file on gmail then use this...
IntentshareIntent=newIntent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, _text);
shareIntent.putExtra (Intent.EXTRA_STREAM, Uri.parse(_file));
startActivity(Intent.createChooser( shareIntent, "Share"));
In this u just have to add text and file Text u attach will become subject in gmail and if you are sharing image on WhatsApp then text will become as image caption
Post a Comment for "Share Pdf File Via Whatsapp From My App On Android"