Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make my share intent support whatsapp and google+

I am using this code to share an image:

File file = ImageLoader.getInstance().getDiskCache().get(imageUrl);
            if (file != null && file.exists()) {
                Uri uri = Uri.fromFile(file);
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.putExtra(Intent.EXTRA_TEXT, "Hello");
                intent.putExtra(Intent.EXTRA_STREAM, uri);
                intent.setType("image/*");
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(Intent.createChooser(intent, "Send"));
            } else {
                Toast.makeText(context, "Image cannot be shared", Toast.LENGTH_SHORT).show();
            }

I used UIL to load the image previously, so mageLoader.getInstance().getDiskCache().get(imageUrl); returns the image file from disk cache.

Gmail, Hangouts, Messages, Drive etc can grab the file but on Google+, the grabbed is not gotten while Whatsapp says "This format is not supported". However if I save the file to Downloads folder and share via Gallery app, the same image is picked by both Google+ and Whatsapp.

like image 435
X09 Avatar asked Jan 22 '26 16:01

X09


1 Answers

You can try to save the file to the external cache, it's working for me. Example with Glide:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setType("image/*");

Glide.with(getContext())
        .load("http://...url.here...")
        .asBitmap()
        .into(new SimpleTarget<Bitmap>(500, 500) {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                try {
                    File file =  new File(getContext().getExternalCacheDir(), "file_to_share.png");
                    file.getParentFile().mkdirs();
                    FileOutputStream out = new FileOutputStream(file);
                    resource.compress(Bitmap.CompressFormat.PNG, 90, out);
                    out.close();

                    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                    sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    getContext().startActivity(Intent.createChooser(sendIntent, ""));
                } catch (IOException e) {
                    Log.e("Share", e.getMessage(), e);
                } finally {

                }
            }
        });
like image 116
Kevin Robatel Avatar answered Jan 24 '26 11:01

Kevin Robatel