通过 Whatsapp 或 Facebook 分享图片和文字 [英] Share image and text through Whatsapp or Facebook

查看:39
本文介绍了通过 Whatsapp 或 Facebook 分享图片和文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有一个分享按钮,我想同时分享图片和文字.在 GMail 中,它运行良好,但在 WhatsApp 中,仅发送图像,而在 Facebook 中,应用程序崩溃.

I have in my app a share button and i want to share an image and a text at the same time. In GMail it works fine but in WhatsApp, only the image is sent and in Facebook the app crashes.

我用来分享的代码是这样的:

The code i use to share is this:

Intent shareIntent = new Intent(Intent.ACTION_SEND);  
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Message");         

Uri uri = Uri.parse("android.resource://" + getPackageName() + "/drawable/ford_focus_2014");
     try {
        InputStream stream = getContentResolver().openInputStream(uri);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

如果我使用 "shareIntent.setType("*/*")" Facebook 和 WhatsApp 崩溃.

If I use "shareIntent.setType("*/ *")" Facebook and WhatsApp crashes.

有没有办法做到这一点?可能同时发送两条消息(WhatsApp).

Is there some way to do this? Maybe sent two messages by separate at the same time (WhatsApp).

提前致谢.

推荐答案

请尝试下面的代码,希望它会起作用.

Please try the below code and hopefully it will work.

    Uri imgUri = Uri.parse(pictureFile.getAbsolutePath());
    Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
    whatsappIntent.setType("text/plain");
    whatsappIntent.setPackage("com.whatsapp");
    whatsappIntent.putExtra(Intent.EXTRA_TEXT, "The text you wanted to share");
    whatsappIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
    whatsappIntent.setType("image/jpeg");
    whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    try {
        activity.startActivity(whatsappIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        ToastHelper.MakeShortText("Whatsapp have not been installed.");
    }

这篇关于通过 Whatsapp 或 Facebook 分享图片和文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆