1

My question how can I send bitmap to Whastapp Application and I use below code;

ImageView iv=(ImageView)view.findViewById(R.id.item_image);
Bitmap bitmap = ((BitmapDrawable)iv.getDrawable()).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.whatsapp");
waIntent.setType("image/png");
waIntent.putExtra(Intent.ACTION_SEND, byteArray);
startActivity(Intent.createChooser(waIntent, "Share with"));

But that code did not work. What is my error ? Thanks.

frogatto
  • 28,539
  • 11
  • 83
  • 129
Ozan
  • 1,191
  • 2
  • 16
  • 31

2 Answers2

9

This worked for me:

public void onClickApp(String pack, Bitmap bitmap) {
    PackageManager pm = context.getPackageManager();
    try {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, "Title", null);
        Uri imageUri = Uri.parse(path);

        @SuppressWarnings("unused")
        PackageInfo info = pm.getPackageInfo(pack, PackageManager.GET_META_DATA);

        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("image/*");
        waIntent.setPackage(pack);
        waIntent.putExtra(android.content.Intent.EXTRA_STREAM, imageUri);
        waIntent.putExtra(Intent.EXTRA_TEXT, pack);
        context.startActivity(Intent.createChooser(waIntent, "Share with"));
    } catch (Exception e) {
        Log.e("Error on sharing", e + " ");
        Toast.makeText(context, "App not Installed", Toast.LENGTH_SHORT).show();
    }
}
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
Ameen Maheen
  • 2,719
  • 1
  • 28
  • 28
  • 1
    You are always using code snippets for normal code. Code Snippets are only supposed to be used for HTML or javascript or other code which can be run in the browser. You cannot run Java in the browser. Use normal code blocks in the future... I will edit your answer for you this time and fix the formatting etc, but please don't do this anymore in the future. This isn't the first time I told you about this... – Xaver Kapeller May 21 '15 at 14:12
  • this require writeexternalstorage permission, how to do it without – Aman Jain Feb 08 '18 at 12:07
  • what is pack String for ? – gumuruh Jan 23 '23 at 15:20
  • 1
    @gumuruh package name for WhatsApp or any other application you wish to send your image to. – Ameen Maheen Jan 30 '23 at 04:37
0
    //pass your image and text(if you want to share) in this method.
    void shareImage(Bitmap bitmap,String text){
    //bitmap is ur image and text is which is written in edtitext
    //you will get the image from the path
    String pathofBmp=
    MediaStore.Images.Media.insertImage(getContentResolver(),
    bitmap,"title", null);    
    Uri uri = Uri.parse(pathofBmp);
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("image/*");
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Star App");
    shareIntent.putExtra(Intent.EXTRA_TEXT, text);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(shareIntent, "hello hello"));
}
santhosh rb
  • 157
  • 2
  • 8