Glide: How to share an Image and Text directly to Whats App in Android-java,using Glide library

Created on 31 Aug 2016  路  15Comments  路  Source: bumptech/glide

How can one share a complete loaded image set in an ImageView along side with text to whats app using intents and Glide.....I looked a lot but i only found for Picasso Library here is the link or if the link is dead google this phrase :

Sharing Content with Intents 路 codepath_android_guides Wiki.htm

question stale

All 15 comments

459? You probably don't want to really share the same image as show in the view, because that image is cropped/fitted/resized for performance. Sharing it would give a bad quality image to the other apps.

If you really want to share the same, use .asBitmap() and save the Bitmap as a file or don't use .asBitmap() and render the view onto a Bitmap and then compress onto a file to be shared in the intent.

@TWiStErRob .... OK i got your it makes a lot of sense. the image would look crappy.

But i have mad a work around the results or image i am sending or sharing to my whats app they seem to be OK i guess it has to do with the glide cache policy i would have mad but below this what i did. i used the glide call back functionality of getting the image and sharing it given that the image or bitmap has loaded fully.

EventfaceImage is the imageview i defined,

Glide.with(context).load(uri).asBitmap().diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate().into(new BitmapImageViewTarget(EventfaceImage) {
            @Override
            public void onResourceReady(final Bitmap bmp, GlideAnimation anim) {
                share.setVisibility(View.VISIBLE);
                EventfaceImage.setImageBitmap(bmp);
                share.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        menu_float.close(true);
                        prepareShareIntent(bmp);
                    }
                });
            }
            @Override
            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                super.onLoadFailed(e, errorDrawable);
            }
        });

then the prepareShareIntent(); function

private void prepareShareIntent(Bitmap bmp) {
        Uri bmpUri = getLocalBitmapUri(bmp); // see previous remote images section
        // Construct share intent as described above based on bitmap

        shareIntent = new Intent();
        shareIntent.setPackage("com.whatsapp");
        shareIntent.setAction(Intent.ACTION_SEND);

        shareIntent.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.shared_via)  );
        shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
        shareIntent.setType("image/*");
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(Intent.createChooser(shareIntent, "Share Opportunity"));

    }

then the getLocalBitmapUri(); it returns a Uri as shown in the method above;

private Uri getLocalBitmapUri(Bitmap bmp) {
        Uri bmpUri = null;
        File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            bmpUri = Uri.fromFile(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return bmpUri;
    }

This works for me in API 22. So i hope this will help someone, but also i have not tested in all the api in Android. If doesn't please let me know and i will see what i can hack. also feel free to better the code and share whats working for you too.

Yep, this is what I meant; a few notes:

  • Click listener writes the file on UI thread.
  • Bitmap compress 90 is weird for PNG as it is ignored; if you have photos (i.e. not clip art), PNG may be huge.
  • You may want to override setResource instead of onResourceReady.
  • You're sharing from a private folder of your app, try FileProvider for more compatibilty (see class JavaDoc for usage).
  • Kudos for not putting your files in public directory or hardcoding the path!
  • startActivity will crash if there's no WhatsApp installed.
  • Decide between chooser and direct package set in intent (I suggest removing package and keeping chooser).
  • Re "seem to be OK": try to share a photo taken with your phone's camera (usually 8+MP and 2MB). Share it to someone both through your app and directly from WhatsApp, compare in full screen on the receiving end.
  • Think about who will delete the file after it's sent, so they don't just accumulate infinitely... consider using one of the cache dirs so the user at least can clear it manually from system UI.

@TWiStErRob this was directed to those who wanted to share to whats app directly as the questions states but i do get what you mean , the developer will have to check first if whats app is installed. and the the developer will have to set a home cleaning mechanism to clear cache off the main thread as it will throw an exception if done on the main UI.

All in all @TWiStErRob thanks for participating in this.

Hello,
onClick is not working, << Its working now after declaring share Button in the OnCreateView.
is there a similar way to share GIF

@keeptesting what's wrong with #459?

@TWiStErRob thank you for your suggestion, i have an error: the file format is not supported, even after implementing new ImageHeaderParser(new ParcelFileDescriptor.AutoCloseInputStream(openFile(uri, "r"))).getType() in ImageFileProvider
and CacheStrategy.All

@kinsleykajiva what is menu_float.close(true);

@SagarKhengat its just a FAB library that i was using to close the child FAB items thus all but has nothing to do with the glide thing

Reopening this as a reminder to look at Glide-Whatsapp interation, no-one really confirmed it working and many complained. It may still take a while until I get to this, but it's in the queue.

This issue has been automatically marked as stale because it has not had activity in the last seven days. It will be closed if no further activity occurs within the next seven days. Thank you for your contributions.

Hello how to send data using intent in android

Hello how to send data using intent in android

Hello @Archirayan41 you can do it by following way.
Source class:

Intent myIntent = new Intent(this, NewActivity.class);
 myIntent.putExtra("firstName", "Your First Name Here");
 myIntent.putExtra("lastName", "Your Last Name Here");
 startActivity(myIntent)

Destination Class (NewActivity class):

protected void onCreate(Bundle savedInstanceState) {    
 super.onCreate(savedInstanceState);     
setContentView(R.layout.view);    
  Intent intent = getIntent();     
 String fName = intent.getStringExtra("firstName");    
 String lName = intent.getStringExtra("lastName"); }

or in regards of the glide image all you can do is:

GlideBitmapDrawable bitmapDrawable = (GlideBitmapDrawable)imageViewPreview.getDrawable();
                            Bitmap bitmap = bitmapDrawable.getBitmap();

                            // Save this bitmap to a file.
                            File cache = getContext().getExternalCacheDir();
                            File sharefile = new File(cache, "toshare.png");
                            Log.d("share file type is", sharefile.getAbsolutePath());
                            try {
                                FileOutputStream out = new FileOutputStream(sharefile);
                                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                                out.flush();
                                out.close();
                            } catch (IOException e) {
                                Log.e("ERROR", String.valueOf(e.getMessage()));

                            }


                            // Now send it out to share
                            Intent share = new Intent(android.content.Intent.ACTION_SEND);
                            share.setType("image/*");
                            share.putExtra(Intent.EXTRA_STREAM,
                                    Uri.parse("file://" + sharefile));

                            startActivity(Intent.createChooser(share,
                                    "Share This Image with"));
Was this page helpful?
0 / 5 - 0 ratings

Related issues

technoir42 picture technoir42  路  3Comments

sergeyfitis picture sergeyfitis  路  3Comments

mttmllns picture mttmllns  路  3Comments

kooeasy picture kooeasy  路  3Comments

PatrickMA picture PatrickMA  路  3Comments