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.
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.
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.
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() .
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 i m getting bitmap from listener.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With