Share image and text through Whatsapp or Facebook

前端 未结 12 907
谎友^
谎友^ 2020-11-28 05:23

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

12条回答
  •  时光取名叫无心
    2020-11-28 05:48

    This worked for me in January 2019

     private void shareIntent() {
    
            Bitmap imgBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
            String imgBitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(),imgBitmap,"title",null);
            Uri imgBitmapUri = Uri.parse(imgBitmapPath);
    
            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            shareIntent.putExtra(Intent.EXTRA_STREAM,imgBitmapUri);
            shareIntent.setType("image/png");
            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            shareIntent.putExtra(Intent.EXTRA_TEXT, "My Custom Text ");
            shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject text");
            startActivity(Intent.createChooser(shareIntent, "Share this"));
        }
    

    This will let the user share the image + text to WhatsApp and all other apps the user wants, it's always the best to let the user select where to share the content instead of prompting just WhatsApp.

    Also make sure that if you include just WhatsApp to share it might not be installed in some devices, for this you will need a try catch and inside of it the startActivity(intent); and also set the package of the intent to just WhatsApp with intent.setPackage("com.whatsapp").

提交回复
热议问题