X09
X09

Reputation: 3946

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.

Upvotes: 3

Views: 457

Answers (2)

X09
X09

Reputation: 3946

In case you're using Universal Image Loader, I applied the accepted answer to save the image and delete it as soon as the user returns from sharing:

private File saveImage(String imageUri, String fileName) {
        File file = new File(this.getExternalCacheDir(), fileName);
        InputStream sourceStream = null;
        File cachedImage = ImageLoader.getInstance().getDiskCache().get(imageUri);
        if (cachedImage != null && cachedImage.exists()) {
            Log.d(TAG, "Cache exists");
            try {
                sourceStream = new FileInputStream(cachedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            Log.d(TAG, "Cache doesn't exist");
        }

        if (sourceStream != null) {
            Log.d(TAG, "SourceStream is not null");
            try {
                OutputStream targetStram = new FileOutputStream(file);
                try {
                    try {
                        IoUtils.copyStream(sourceStream, targetStram, null);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } finally {
                    try {
                        targetStram.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    sourceStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            Log.d(TAG, "SourceStream is null");
            Toast.makeText(this, "Image cannot be shared", Toast.LENGTH_SHORT).show();
        }
        return file;
    }

 private void shareImage(String imageUrl, String fileName) {
         if (isSDReadableWritable()) {
             file = saveImage(imageUrl, fileName);
             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);
             startActivityForResult(Intent.createChooser(intent, "Send"), 20);
         } else {
             Toast.makeText(this, "Storage cannot be accessed", Toast.LENGTH_SHORT).show();
         }
 }

To delete the file just override onActivityResult and it'll be deleted immediately after sharing

@Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 20 && file != null) {
            boolean isDelete = file.delete();
            Log.d(TAG, "isDelete is " + isDelete);
        }
     }

Upvotes: 0

Kevin Robatel
Kevin Robatel

Reputation: 8386

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 {

                }
            }
        });

Upvotes: 1

Related Questions