Share text OR Image On Whatsapp in Android

主宰稳场 提交于 2021-02-07 04:37:14

问题


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?


回答1:


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();
}



回答2:


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.");
}



回答3:


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.



来源:https://stackoverflow.com/questions/24605491/share-text-or-image-on-whatsapp-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!