Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

android - Share image and text through Whatsapp or 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);

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

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

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...