Search code examples
androidfacebookwhatsappshare-intent

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.


Solution

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