I'm sharing my images via WhatsApp - but I have to choose the recipient.
Here is my code:
public static void shareImage(Context context,String path, String text, String otherAppPackage){
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.setPackage("com.whatsapp");
share.putExtra(android.content.Intent.EXTRA_SUBJECT, getSubject(context));
if (text!=null){
share.putExtra(Intent.EXTRA_TEXT,text);
}
if (path!=null){
share.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(new File(path)));
}
context.startActivity(Intent.createChooser(share, context.getString(R.string.share_via)));
}
I wold like to share with someone directly. Does some of you know how can I do this.
Thanks
You can use Intent.ACTION_SENDTO, but message is not copied to clipboard then:
Uri uri = Uri.parse("smsto:+123456789");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.setPackage("com.whatsapp");
it.putExtra("sms_body", "The SMS text");
it.putExtra("chat",true);
startActivity(it);
Related
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 code
String PLACEHOLDER = "file:///android_asset/QUOT_2016_10(test).pdf";
File f = new File(PLACEHOLDER);
Uri uri = Uri.fromFile(f);
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_TEXT, "hi");
share.setPackage("com.whatsapp");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setType("application/pdf");
activity.startActivity(share);
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:
File outputFile = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOWNLOADS), "ref Number from Quotation.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("com.whatsapp");
activity.startActivity(share);
File outputPath= new File(Environment.getExternalStorageDirectory() + "/MyPdf/example.pdf");//change with your path
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
Uri fileUri = 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"));
It's technically wrong, what if someone has WhatsApp business or want to share file on gmail then use this...
Intent shareIntent = new Intent(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
I am trying to launch whatsapp app from my app and passing some text to a particular number. Whatsapp App is launching and but text is not able to pass.
String smsNumber = "98*******3";
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra(Intent.EXTRA_TEXT, textWithClickableLink);
i.setPackage("com.whatsapp");
mContext.startActivity(i);
Is there anything missing in my code?
Please help me on this.
Thanks in advance!
Try this one:
Uri uri = Uri.parse("smsto:" + mobileno);
Intent whatsappIntent = new Intent(Intent.ACTION_SENDTO, uri);
whatsappIntent.setPackage("com.whatsapp");
String str = "https://play.google.com/store/apps/details?id=com.example.activity&hl=en";
whatsappIntent.putExtra("sms_body", str);
try {
activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
// Whatsapp have not been installed
}
you can try this method, it should work. I have tested
private void shareWhatsApp() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "text to share");
//if you have any images to share sue this, uri is the uri of image
//shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
//shareIntent.setType("image/*"); for image sharing
shareIntent.setType("text/plain");
shareIntent.setPackage("com.whatsapp");
startActivity(shareIntent);
}
This will launch WhatsApp application and you have to manually select the contacts and send. since they dont provide any SDK we cant send automatically.
I tried this code. It makes my app to open list of all app by which I can send messages. After selecting any app now I am able to send the message that I am attaching with intent.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "From Stockal App");
intent.putExtra(Intent.EXTRA_TEXT, text);
mContext.startActivity(intent);
Thank you all for your response.
I wanted to know how I can send text and image to a specific whatsapp contact. I found some code to view a specific contact, but not to send data.
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("sms_body", smsText);
i.setPackage("com.whatsapp");
startActivity(i);
But that code just opening the chat history, but doesn't take the text and image and send it.
I also try the below code for send image and text via whatsApp but that ask for choose the contact for send
Intent shareIntent = new Intent();
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("*/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, sendString);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent. setPackage ("com. whatsapp");
startActivity(shareIntent);
If that functionality is possible then please give me a suggestion for this.
Solution :
Intent sendIntent = new Intent("android.intent.action.SEND");
File f=new File("path to the file");
Uri uri = Uri.fromFile(f);
sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.ContactPicker"));
sendIntent.setType("image");
sendIntent.putExtra(Intent.EXTRA_STREAM,uri);
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("919xxxxxxxxx")+"#s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT,"sample text you want to send along with the image");
startActivity(sendIntent);
Refer to my answer at for detailed explanation.
https://stackoverflow.com/a/41805567/3989718
I would love to share a picture with a text or url.
I'm using this code but I am only sharing the image, and changing the order of the "type" makes me share both but only as email / gmail
What am I doing wrong? my code is:
edit1
Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE);
share.setType("*/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Wallpaper/1.jpg"));
share.putExtra(Intent.EXTRA_TEXT, "helloworld");
startActivity(Intent.createChooser(share, (getApplicationContext().getString(R.string.condividi))));
try this
File DoutFile = new File(path);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(DoutFile));
share.putExtra(Intent.EXTRA_TEXT,
"" + getResources().getString(R.string.app_name));
startActivity(Intent.createChooser(share, "Share Image"));
Specify MIME type also for the text. "text/plain" is the type of text data MIME. Try using "*/*" as MIME, so you can send any generic data type.
Also try changing ACTION_SEND to ACTION_SEND_MULTIPLE which specialized for delivering multiple data.
More info about ACTION_SEND_MULTPLE and handling MIME types:
http://developer.android.com/reference/android/content/Intent.html
If you want to send multiple files then use the following code :-
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_SUBJECT, "Add any subject");
intent.setType("image/*"); /* sharing any type of image. Modify to your need */
ArrayList<Uri> files = new ArrayList<Uri>();
for(String path : filesToSend /* List of images you want to send */) {
File file = new File(path);
Uri uri = Uri.fromFile(file);
files.add(uri);
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files); /* adding files to intent */
startActivity(intent);
I have my images in drawable folder and I am getting that images in gridview.Also I am showing the perticular image on selection fullscreen.Now I want to send MMS with image attachment.
Here is my code to show the image. And to send MMS.how to get Uri to put to intent or how to send the image attachment.
imageView.setImageResource(imageAdapter.mThumbIds[position]);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra("sms_body", "Hii");
sendIntent.setType("image/png");
startActivity(sendIntent);
Use the following code to send the mms.. Hope this wil help you.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", "Hi how are you");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("/sdcard/file.gif")));
intent.setType("image/gif");
startActivity(Intent.createChooser(intent,"Send"));
Uri uri = Uri.parse("android.resource://your.package.here/drawable/image_name");
EDIT:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", "Hi how are you");
Uri uri = Uri.parse("android.resource:// com.example.stack/drawable/ic_launcher.png");
intent.putExtra(Intent.EXTRA_STREAM,uri );
intent.setType("image/png");
startActivity(Intent.createChooser(intent,"Send"));