8

I would like to use Glide to load bitmap to ImageView after cropping and re-sizing a bitmap.

I don't want to use ImageView.setImageBitmap(bitmap); because I am loading lots of images and it might be taking up some memory, although the images are small in size, I just need to use Glide because I know it optimises image caching.

I read this post, but I didn't quite understand his solution when I tried implementing it. So maybe someone has a cleaner and more easily understandable solution.

This is my code which picks up an image and creates a bitmap out of it.

I need to use glide instead of ImageView.setImageBitmap(bitmap);.

new AsyncTask<String, Void, Void>() {
    Bitmap theBitmap = null;
    Bitmap bm = null;

    @Override
    protected Void doInBackground(String... params) {
        String TAG = "Error Message: ";
        try {
            //Load the image into bitmap
            theBitmap = Glide.
                    with(mContext).
                    load("http://example.com/imageurl").
                    asBitmap().
                    into(-1, -1).
                    get();

            //resizes the image to a smaller dimension out of the main image.
            bm = Bitmap.createBitmap(theBitmap, 0, 0, 210, 80);
        } catch (final ExecutionException e) {
            Log.e(TAG, e.getMessage());
        } catch (final InterruptedException e) {
            Log.e(TAG, e.getMessage());
        } catch (final NullPointerException e) {
            //
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void dummy) {
        if (null != theBitmap) {
            //Set image to imageview.
            **// I would like to Use Glide to set the image view here Instead of .setImageBitmap function**
            holder.mImageView.setImageBitmap(bm);

            holder.mImageView.setAdjustViewBounds(true);
            holder.mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }
    }
}.execute();
Community
  • 1
  • 1
Tosin Onikute
  • 3,883
  • 6
  • 38
  • 61

1 Answers1

18

You don't need AsyncTask to load image with Glide. Glide load image asynchronys. Try to use this code:

Glide.with(mContext)
                .load("http://example.com/imageurl")
                .asBitmap()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        // you can do something with loaded bitmap here

                        // .....

                        holder.mImageView.setImageBitmap(resource);
                    }
                });
Yuriy Kolbasinskiy
  • 3,791
  • 3
  • 16
  • 33
  • No, I want to Crop the Image to TOP, not centerCrop, that is why I choose to create a Bitmap out of it and then Give it ( 0, 0, 210, 80 ) so that It would crop to that size and start from the Top instead of Center. – Tosin Onikute Nov 14 '15 at 14:58
  • I have edited my answer, please check this. p.s. Are you using constants values (e.g. 210,80) to start Bitmap on Top? I think it's bad practic and you should use custom `Drawable` with top aligment. – Yuriy Kolbasinskiy Nov 14 '15 at 15:20
  • Thanks but this didn't work either, When you create your bitmap inside onResourceReady, It still does the same thing as when its outside onResourceReady. No changes! – Tosin Onikute Nov 14 '15 at 15:24
  • 1
    I use constant values because thats the size expected to be cropped out of the real image, and not just any size – Tosin Onikute Nov 14 '15 at 15:25
  • Can you post a new code with my changes? Can't understand what is the problem. And if you can, please attach some screenshots or pictures. – Yuriy Kolbasinskiy Nov 14 '15 at 15:27
  • 1
    I am getting this error sometimes - **Bitmap too large to be uploaded into a texture** – Narendra Singh Aug 16 '16 at 08:17
  • Firstly sometimes large image will not be loaded with this implementation. You'd better use SimpleTarget as an instance variable rather than as anonymous class (to prevent from being garbage collected). Secondly you can make glide to scale the image once it is downloaded (same as it does for you by default when you load directly into the ImageView). For this you can specify target size during the initialization: SimpleTarget target = new SimpleTarget(500, 300) – Leo DroidCoder Jun 01 '17 at 07:47