Home > OS >  How to Share both image and text through whatsapp android programatically
How to Share both image and text through whatsapp android programatically

Time:01-26

I need to share both an image and some text via WhatsApp, using an Intent on Android.

Uri imageUri = Uri.parse(Filepath);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.whatsapp");
shareIntent.putExtra(Intent.EXTRA_TEXT, "My sample image text");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imageUrl));
shareIntent.setType("image/png");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

try {
    startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
    ToastHelper.MakeShortText("Kindly install whatsapp first");
}

I’m using the code above, but it throws a 'File format not supported' error while sharing.

CodePudding user response:

You can try like this

  public static void shareOnWhatsapp(Context context, String str) {
    Uri parse = Uri.parse(str);
    Intent intent = new Intent();
    intent.setAction("android.intent.action.SEND");
    intent.setPackage("com.whatsapp");
    String stringBuilder = context.getResources().getString(R.string.play_more_app)  
            context.getPackageName();
    intent.putExtra("android.intent.extra.TEXT", stringBuilder);
    intent.putExtra("android.intent.extra.STREAM", parse);
    intent.setType("image/*");
    intent.addFlags(1);
    try {
        context.startActivity(intent);
    } catch (Exception unused) {
        setToast(context, context.getResources().getString(R.string.whatsapp_not_installed));
    }
}

I used this function to share images it's working I hope it's work you too.

Edit

  1. Add this dependency for converting URL to bitmap

        implementation 'com.github.bumptech.glide:glide:4.14.2'
    
  2. Use this function to get Image URI to share

        private Uri getImageURI(Bitmap image) {
        File imagesFolder = new File(getCacheDir(), "images");
        Uri uri = null;
        try {
            imagesFolder.mkdirs();
            File file = new File(imagesFolder, "shared_image.png");
    
            FileOutputStream stream = new FileOutputStream(file);
            image.compress(Bitmap.CompressFormat.PNG, 90, stream);
            stream.flush();
            stream.close();
            uri = FileProvider.getUriForFile(this, getPackageName()   ".provider", file);
    
        } catch (IOException e) {
            Log.d("TAG", "IOException while trying to write file for sharing: "   e.getMessage());
        }
        return uri;
    }
    
  3. Use this function for share Image on WhatsApp

        public static void shareOnWhatsapp(Context context, Uri uri) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.SEND");
        intent.setPackage("com.whatsapp");
        String stringBuilder = "Your String Or Message"  
                context.getPackageName();
        intent.putExtra("android.intent.extra.TEXT", stringBuilder);
        intent.putExtra("android.intent.extra.STREAM", uri);
        intent.setType("image/*");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        try {
            context.startActivity(intent);
        } catch (Exception unused) {
            Toast.makeText(context, "whatsapp not installed", Toast.LENGTH_SHORT).show();
        }
    }
    
  4. Uses like this

            findViewById(R.id.btnShare).setOnClickListener(v -> {
            Glide.with(this).asBitmap().load( /*Your URL*/ "https://drinkprime.in/images/smart_water_purifier.jpg").into(new CustomTarget<Bitmap>() {
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    Bitmap bitmap = resource;
                    shareOnWhatsapp(MainActivity.this, getImageURI(bitmap));
                }
    
                @Override
                public void onl oadCleared(@Nullable Drawable placeholder) {
                }
            });
        });
    

It's Working now completely

  • Related