Glide : How to get the error bitmap? - android

By using the Glide image loader library, you are able to be notified if the began loading has succeeded.
Glide
.with(this)
.load(uri)
.asBitmap()
.listener(mRequestListener)
.into(imageView);
It works well, because you can get the bitmap loaded by Glide on the onResourceReady callback method of the RequestListener class. Now use an error placeholder:
Glide
.with(this)
.load(uri)
.asBitmap()
.listener(mRequestListener)
.error(R.drawable.error_image)
.into(imageView);
What if I want to get the error image?
When the given image couldn't be found and my error image loads, only the onException callback method gets called and hence this, we can't get the error bitmap.

Are you looking for something like this:
Glide.with(context)
.load(uri)
.asBitmap()
.into(new SimpleTarget<Bitmap>(width, height) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
// add image to the imageView here
}
#Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
// you are given the error drawable
}
});

Related

Android - Glide in the recyclerView

I am new in android developing and i have an issue. I try to load some images in a RecyclerView. The incoming data have the type byte[].
My first solution is converting byte[] to Bitmap and it works fine but the bitmaps stucks in the memory and i was getting an OutOfMemory exception.
The second solution is using Glide.
Glide.with(mContext)
.load(field.getImage())
.into(holder.mImageView);
But with this solution i get same images in each item.
I think the problem is in the Glide's cache because incoming arrays are different. I try to do solve this with this code:
requestOptions.skipMemoryCache(true);
requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(mContext)
.applyDefaultRequestOptions(requestOptions)
.load(field.getImage())
.into(holder.mImageView);
}
But it doesn't help. What is the right way to do this?
try this use setDefaultRequestOptions(requestOptions)
Glide.with(context)
.setDefaultRequestOptions(new RequestOptions().placeholder(R.drawable.booked_circle).error(R.drawable.booked_circle))
.load(url)
.into(imageView);
you can resize image like this
Glide.with(PhotoGalleryActivity.this)
.using(new FirebaseImageLoader())
.load(storageReference)
.asBitmap()
.into(new SimpleTarget<Bitmap>(520, 520) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
holder.img.setImageBitmap(resource);
}
});
For resizing image and adding (placeholder and error image) use this code:
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.app_icon);
requestOptions.error(R.drawable.ic_image_error);
Glide.with(context)
.setDefaultRequestOptions(requestOptions)
.asBitmap()
.load(newsContentModel.getImgNewsUrl())
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
// requestLayout()
// Call this when something has changed which has
// invalidated the layout of this view.
myViewHolder.imgImage.requestLayout();
myViewHolder.imgImage.getLayoutParams().height = newHeight;
myViewHolder.imgImage.getLayoutParams().width = newWidth;
// Set the scale type for ImageView image scaling
myViewHolder.imgNewsImage.setScaleType(ImageView.ScaleType.FIT_XY);
myViewHolder.imgImage.setImageBitmap(bitmap);
}
});

Load image synchronously with glide and cache it in the disk

I'm doing something like this to get bitmap synchronously and set it as image by using imageView.setImageBitmap(bitmap);
return Glide
.with(mContext)
.load(url)
.asBitmap()
.into(width, height)
.get();
I need to do it synchronously because I'm doing it inside AsyncTask's doInBackground. I need to fetch image url from the backend before loading it with glide. 
Later I'm loading the same url into view directly with glide.
The problem is that image is not cached in the disk. 
Any solution for this?
Use diskCacheStrategy
return Glide
.with(mContext)
.load(url)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(width, height)
.get();
add diskCacheStrategy to your Glide, Select StrategyType as Source
You can have idea why to use SOURCE from this link
DiskCacheStrategy.NONE caches nothing, as discussed
DiskCacheStrategy.SOURCE caches only the original full-resolution image.
DiskCacheStrategy.RESULT caches only the final image, after reducing the resolution (and possibly transformations) (default behavior)
DiskCacheStrategy.ALL caches all versions of the image
try:
to get Bitmap From URL:
return Glide
.with(mContext)
.load(url)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(width, height)
.get();
For loading in ImageView :
Glide
.with(mContext)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(imageView) // your ImageViewID here
For loading Image into ImageView after getting Bitmap:
Glide.with(context)
.load(url)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
iv_image.setImageBitmap(resource); // you can store resource to some variable too if you want to update more than 1 images or try it like iv_image2.setImageBitmap(resource)
iv_image.requestLayout();
}
});
I have faced this issue many times the best way to overcome it is by using interfaces to load data after it is fetched inside glide CustomTarget
Create a new file called OnglideLoaded.java file like this:
import android.graphics.drawable.Drawable;
public interface OnGlideLoaded {
void onGlideLoaded(Drawable drawable);
}
Now create setter method for it like this where glide is:
public void setOnGlideLoaded(OnGlideLoaded onGlideLoaded) {
this.onGlideLoaded = onGlideLoaded;
}
Not call it inside Glide Target:
Glide.with(context)
.asBitmap()
.load(uri)
.into(new CustomTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
icon = resource;
if (onGlideLoaded!=null){
//This will send the image to every registered interface
onGlideLoaded.onGlideLoaded(icon);
}
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
}
});
Now call the interface like this where you want the image:
reference.setOnGlideLoaded(new OnGlideLoaded() {
#Override
public void onGlideLoaded(Drawable drawable) {
imageView.setImageBitmap(drawable);
}
});
Here, reference will be the object where you created the setter method.

Bitmap take too much time to load

I am trying to implement load bitmap in ImageView, but it is taking too much time to load. I have also used ImageLoader but it is only supports for url, it is not supports for bitmap, so how can I show bitmap instantly? Please help to solve it!
Show image using Image loader
For url I used this code
imgLoader.DisplayImage(mImage, R.mipmap.ic_launcher, imgProfile);
You can use any ImageLoader library like Glide, Picasso
Here is the snippet using Glide
Glide.with(mContext)
.load(your_image_url_or_resource_id)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
// you can do something with loaded bitmap here
yourImageViewRef.setImageBitmap(resource);
}
});
If you want to load image using url try this code
Glide.with(context).load(your_image_url_or_resource_id).placeholder(R.drawable.placeholder_icon)
.error(R.drawable.error_icon).into(yourImageViewRef);

Android Glide - Check if a bitmap identified by a URL is cached

I am using Glide to display an image from internet in a notification, not in an image view. In order to do this I extended SimpleTarget like this :
Glide
.with(context.getApplicationContext())
.load(imageUrl)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap largeIcon, GlideAnimation glideAnimation) {
createNotif(largeIcon);
nmc.notify(notificationId, notification);
}
});
It works fine.
In order to speed up things, I would like to check the Glide cache on a specific Image URL first and do the asynchronous query only if the bitmap is not in the cache. Something like this (glideCache and getOnlyIfinCache is what I am looking for) :
bitmap=null;
bitmap = glideCache.getOnlyIfinCache(ImageURL);
if (bitmap!=null) { // the bitmap is available
createNotif(bitmap);
nmc.notify(notificationId, notification);
} else { // the bitmap is not available. Need to get it on internet
Glide
.with(context.getApplicationContext())
.load(imageUrl)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap largeIcon, GlideAnimation glideAnimation) {
createNotif(largeIcon);
nmc.notify(notificationId, notification);
}
});
}

Picasso's Target and centerCrop() and fit()

I have an image on my server and I want to display it using Picasso on my Android client.
I want to add a default image when the image is loading on Picasso so I am using Target as follows:
Picasso.with(UserActivity.this).load(imageUri.toString()).transform(new RoundedTransformation(500, 1)).into(
new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
userPic.setImageBitmap(bitmap);
}
#Override
public void onBitmapFailed(Drawable drawable) {
userPic.setImageBitmap(defaultDrawable);
}
#Override
public void onPrepareLoad(Drawable drawable) {
userPic.setImageBitmap(defaultDrawable);
}
});
I want to centerCrop() and fit() this image but it gives me an error and it tells me that I cant use them with Target. Is there anyway to use these features on Picasso? Why don't they allow these two functions with Target?
You don't need to use Target to accomplish your goal.
Side note, I am not certain that you can actually use both fit() and centerCrop() together.
See this example:
Picasso.with(context)
.load(url) // Equivalent of what ends up in onBitmapLoaded
.placeholder(R.drawable.user_placeholder) // Equivalent of what ends up in onPrepareLoad
.error(R.drawable.user_placeholder_error) // Equivalent of what ends up in onBitmapFailed
.centerCrop()
.fit()
.into(imageView);
Try this
Picasso.with(context)
.load(url)
.resize(50, 50)
.centerCrop()
.fit()
.placeholder(defaultImageLink)
.error(R.drawable.user_placeholder_error)
.transform(new RoundedTransformation(500, 1))
.into(imageView)
We can also resize the image as required by the imageview which will save memory usage if the image too large.
Callback method can be used to hide the progress bar and show some text within the image View on image load fail.
Picasso.with(context)
.load(url)
.placeholder(R.drawable.placeholder_img)
.error(R.drawable.error_img)
.resize(450, 420)
.centerCrop()
.fit()
.into(imageView, new Callback() {
#Override
public void onSuccess() {
progressBar.setVisibility(View.GONE);
}
#Override
public void onError() {
progressBar.setVisibility(View.GONE);
image_failed_text.setVisibility(View.VISIBLE);
}
});

Categories

Resources