Share text OR Image On Whatsapp in Android - android

I am developing an application in which i have to share image and text on WhatsApp with in my application.
In IOS i have this code
NSURL *whatsappURL = [NSURL URLWithString:#"whatsapp://send?text=Hello%2C%20World!&abid=143rnjk4545352523"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
}
How i will be able to do this in Android?Is there any possiblity to share text and images on whatsapp from Android app?

You can use Whatsapp intent to do so.
Note :- WhatsApp does not support messages with both pictures and text,so use below code to share text.
Share Text on Whatsapp
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Hello World");
try {
activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}
Share Image on Whatsapp
Intent whatsappIntent = new Intent(android.content.Intent.ACTION_SEND);
whatsappIntent.setType("image/*");
whatsappIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file));//add image path
startActivity(Intent.createChooser(share, "Share image using"));
Update
Whatsapp now support text (consider as image caption) with image as
Intent whatsappIntent = new Intent(android.content.Intent.ACTION_SEND);
whatsappIntent.setType("image/*");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Hello World");
whatsappIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file)); //add image path
startActivity(Intent.createChooser(share, "Share image using"));
try {
activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(activity, "Whatsapp have not been installed.", Toast.LENGTH_SHORT).show();
}

Currently Whatsapp supports Image and Text sharing at the same time. (Nov 2014).
Here is an example of how to do this:
/**
* Show share dialog BOTH image and text
*/
Uri imageUri = Uri.parse(pictureFile.getAbsolutePath());
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
//Target whatsapp:
shareIntent.setPackage("com.whatsapp");
//Add text and then Image URI
shareIntent.putExtra(Intent.EXTRA_TEXT, picture_text);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}

It is Possible now for Whatsapp Chack your self.
As of Now Whatsapp Support share Image and text together.
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT,title + "\n\nLink : " + link );
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(sharePath));
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share image via:"));
Image will be as it is and pass text as EXTRA_TEXT and it will be shown as caption.

Related

Instagram sharing issue: Always I get the message “Unable to load image”

I'm working on share functionality
I 've a problem with Instagram Share
Here is my code :
I'm trying share "account" image which is stored in "drawable" folder.This is just an example
try {
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.parse("android.resource://com.example.swathi.booklender/drawable/account");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setPackage("com.instagram.android");
startActivity(shareIntent);
}catch (Exception e)
{
Toast.makeText(getActivity(),"No Activity available, install instagram",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
How to I get rid of this problem?
You can't share drawable directly
First create a file from drawable then share it
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
File image = new File(getFilesDir(), "foo.jpg");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image));
startActivity(Intent.createChooser(shareIntent, "Share image via"));

How to send picture with whatsapp android

Hi everyone I'm an android develeloper and in my app I need to share an image with a text by whatsapp, the problem is that Uri method doesn't work anymore. This is Uri method:
Uri imageUri = Uri.parse(pictureFile.getAbsolutePath());
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
//Target whatsapp:
shareIntent.setPackage("com.whatsapp");
//Add text and then Image URI
shareIntent.putExtra(Intent.EXTRA_TEXT, picture_text);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}
So is there any method different from this one?

how to share image from sever to whatsapp

hey guys i want to share image from server to whatsapp but its give an error image share failed can you tell me can i share image directly server to whatsapp what i am doing something wrong
this is my code
Intent shareIntent = new Intent();
Uri imageUri = Uri.parse("http://stacktoheap.com/images/stackoverflow.png"); // here is my selected url
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.whatsapp");
shareIntent.putExtra(Intent.EXTRA_TEXT, img_txt);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
context.startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.whatsapp")));
}
help me friends
my answer is simple first download image from sever and then share image on whatsapp
try this code
String imagePath =Environment.getExternalStorageDirectory() + "your folderpath"+"imagename";
File f=new File(imagePath);
Uri uri = Uri.fromFile(f);
Intent share = new Intent(Intent.ACTION_SEND);
share.setPackage("com.whatsapp");
share.setType("image/jpg");
share.putExtra(Intent.EXTRA_TEXT,"your text"); //want share text with image
share.putExtra(Intent.EXTRA_STREAM, uri);
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(share);
} catch (android.content.ActivityNotFoundException ex) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.whatsapp")));
}
You are getting issue because you are sharing image and text both
Infact whatsapp doesn't allow to share image and text simultaneously, try either image only

Share of Whatsapp and facebook along with text and image

I wanted to share my image to social site along with textlink and image , I am using below code , it does share Image but no text , Also I tried to set setype to "text/html" , it does not does what i need.
protected void onPostExecute(String args) {
// TODO Auto-generated method stub
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_TITLE, "http://www.google.com");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getAbsolutePath()));
startActivity(Intent.createChooser(share, "Share Image"));
pDialog.dismiss();
}
Give this a try
Uri imageUri = Uri.parse(Filepath);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.whatsapp");
shareIntent.putExtra(Intent.EXTRA_TEXT, "My sample image text");
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Kindly install whatsapp first");
}
PS: For sharing in multiple apps just create an alert dialogue and add
whtsapp,fb and other sharing option and while clicking on them you
have to call specific intent for this.As per my knowledge for sharing
in facebook you must impliment it's sdk, though facebook is able to
share simple image and hyperlink without integrating sdk.

Android WhatsApp image sharing

I am developing an application in which i have to share image and text on WhatsApp with in my application.
Is there any best way to do that.
I am capturing my application screen, and need to send that image via WhatsApp
After Spending some time I am able to share Image and Text to whatsapp from my application using this code :
String smsNumber = "91xxxxxxxxxx"; //without '+'
try {
Uri imageUri = null;
try {
imageUri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(),
BitmapFactory.decodeResource(getResources(), R.drawable.whatsapp_image), null, null));
} catch (NullPointerException e) {
}
Intent shareIntent = new Intent("android.intent.action.MAIN");
shareIntent.setType("*/*");
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "App - Link");
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra("jid", smsNumber + "#s.whatsapp.net"); //phone number without "+" prefix
shareIntent.setPackage("com.whatsapp");
startActivity(Intent.createChooser(shareIntent, "send"));
} catch (Exception e) {
}
Happy Coding :)

Categories

Resources