简体   繁体   中英

Getting Bitmap from Image using Glide in android

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.

Bitmap chefBitmap = Glide.with(MyActivity.this)
.asBitmap()
.load(chef_image)
.submit()
.get();

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 im getting bitmap from listener.

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;
        }
    });

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM