Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 951
Anupam Avatar asked Aug 18 '17 06:08

Anupam


People also ask

How do you download pictures from Glide?

To simply load an image to LinearLayout, we call the with() method of Glide class and pass the context, then we call the load() method, which contains the URL of the image to be downloaded and finally we call the into() method to display the downloaded image on our ImageView.

Which is better glide or Picasso?

Glide is faster and the results of Picasso and Coil are similar. But what about when we are loading from the cache. As you can see in the images below we have the best times for Glide in most of the cases.

Does glide run on main thread?

The into(ImageView) method of Glide requires you to call it only on main thread, but when you pass the loading to a Timer it will be executed in a background thread. What you can do is to retrieve a bitmap by calling get() instead of into() and then set that bitmap on the ImageView by calling setImageBitmap() .


2 Answers

Bitmap chefBitmap = Glide.with(MyActivity.this)
.asBitmap()
.load(chef_image)
.submit()
.get();
like image 95
Anand Khinvasara Avatar answered Oct 18 '22 23:10

Anand Khinvasara


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.

like image 22
Ashu Kumar Avatar answered Oct 18 '22 23:10

Ashu Kumar