13

I want to get the bitmap from an image using Glide. I am doing the following -

Bitmap chefBitmap = Glide.with(MyActivity.this)
.load(chef_image)
.asBitmap()
.into(100, 100)
.get();

It used to work with the previous Glide version. But it does not work with this in gradle - "compile 'com.github.bumptech.glide:glide:4.0.0'"

I want to use this dependency because this is the latest version.

Can anyone help me in this regard. Thanks in advance.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Anupam
  • 2,845
  • 2
  • 16
  • 30
  • if you are using latest android studio you need to add annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0' also – user3040153 Nov 20 '17 at 10:07

5 Answers5

28
Bitmap chefBitmap = Glide.with(MyActivity.this)
.asBitmap()
.load(chef_image)
.submit()
.get();
Anand Khinvasara
  • 618
  • 4
  • 17
16

There is little changes according to latest version of Glide. Now we need to use submit() to load image as bitmap, if you do not call submit() than listener won't be called. In 4.0 Version, submit() added and in order to invoke listener. One of user commented code is working with GlideApp. You can use below code to run with GlideApp if you are using.

here is working example i used today.

Glide.with(cxt)
  .asBitmap().load(imageUrl)
  .listener(new RequestListener<Bitmap>() {
      @Override
      public boolean onLoadFailed(@Nullable GlideException e, Object o, Target<Bitmap> target, boolean b) {
          Toast.makeText(cxt,getResources().getString(R.string.unexpected_error_occurred_try_again),Toast.LENGTH_SHORT).show();
          return false;
      }

      @Override
      public boolean onResourceReady(Bitmap bitmap, Object o, Target<Bitmap> target, DataSource dataSource, boolean b) {
          zoomImage.setImage(ImageSource.bitmap(bitmap));
          return false;
      }
  }
).submit();

It is working and i m getting bitmap from listener.

Ashu Kumar
  • 832
  • 19
  • 38
8

You need to set the size with RequestOptions in apply() and use a RequestListener to retrieve the bitmap. The mthod asBitmap() needs to be called before load(). So it will look like this:

Glide.with(getContext().getApplicationContext())
    .asBitmap()
    .load(chef_image)
    .apply(new RequestOptions().override(100, 100))
    .listener(new RequestListener<Bitmap>() {
        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
            return false;
        }

        @Override
        public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
            // resource is your loaded Bitmap
            return true;
        }
    });
Björn Kechel
  • 7,933
  • 3
  • 54
  • 57
1

You should add in your

dependencies{
  compile 'com.github.bumptech.glide:glide:4.0.0'
  compile 'com.android.support:support-v4:25.3.1'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0'

Also, give permission in your manifest.xml

Zafar Kurbonov
  • 2,077
  • 4
  • 19
  • 42
-2

Try this in your build.gradle;

 compile 'com.github.bumptech.glide:glide:3.7.0'

and load your bitmap like below;

  Glide.with(activity).load(m.getThumbnailUrl())
            .thumbnail(0.5f)
            .crossFade()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(imageview);
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57