将图像从一个应用程序共享到Whatsapp

时间:2014-06-16 10:52:21

标签: java android whatsapp

我想将我的应用程序中的图像分享给Whatsapp,但我无法做到这一点。我将所有图像放入资源文件夹。我需要改变什么?

public void onClickWhatsApp() {

        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");
                String text = "Hey, check out this cool game for Android ‘Free Ticket Bollywood Quiz’ www.globussoft.com";
        waIntent.setPackage("com.whatsapp");
        if (waIntent != null) {
            waIntent.putExtra(Intent.EXTRA_TEXT, text);//
            startActivity(Intent.createChooser(waIntent, "Share with"));
        } else {
            Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                    .show();
        }
}

1 个答案:

答案 0 :(得分:3)

您的标题是关于分享图片,但您尝试共享文字。好。尝试其中任何一个。

尝试此操作来分享文字:

Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "This is a test text");
try {
    activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
    ToastHelper.MakeShortText("Whatsapp have not been installed.");
}

尝试此操作来分享图片:

Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
Uri uri=Uri.parse("file:///android_asset/myimage.png"); 
whatsappIntent.setType("image/*");
whatsappIntent.setPackage("com.whatsapp");
sendIntent.putExtra(Intent.EXTRA_STREAM,uri);
try {
    activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
    ToastHelper.MakeShortText("Whatsapp have not been installed.");
}

希望它有所帮助。