在Android上的Whatsapp上分享文字或图片

时间:2014-07-07 07:51:17

标签: android messages whatsapp

我正在开发一个应用程序,我必须在我的应用程序中与 WhatsApp 共享图像和文本。

在IOS中,我有这段代码

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!&abid=143rnjk4545352523"];                                                             
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
}

我将如何在Android中执行此操作?是否有可能在Android应用程序的whatsapp上共享文本和图像?

3 个答案:

答案 0 :(得分:12)

您可以使用Whatsapp意图来执行此操作。

注意: - WhatsApp不支持包含图片和文字的邮件,因此请使用以下代码共享文字。

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

在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"));

<强>更新

Whatsapp现在支持文本(视为图像标题),图像为

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

答案 1 :(得分:1)

目前Whatsapp同时支持图像和文本共享。 (2014年11月)。

以下是如何执行此操作的示例:

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

答案 2 :(得分:0)

现在Whatsapp Chack你的自我是可能的。

截至目前,Whatsapp支持分享图像和文字。

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:"));

图像将保持原样,并将文本作为EXTRA_TEXT传递,它将显示为标题。