Glide: error image is not showing while using thumbnail - android

I'm using the 3.7.0 version of Glide.
I’m using a thumbnail to show the gif as a loading image and error method to show an error image in the case of the network issue. But, imageview is not showing the error image. It is only showing the thumbnail.
I want to replace the thumbnail with an error image in case of any error.
Here is my code:
fun ImageView.setImageUrlWithThumbnail(
url: String?,
#DrawableRes thumbnail: Int = R.drawable.image_loading,
#DrawableRes error: Int = R.drawable.image_download_failed
) {
url.let {
Glide.with(this.context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.thumbnail(Glide.with(this.context).load(thumbnail).error(error))
.error(error)
.into(this)
}
}

Related

How to re-load a local manually cached Bitmap with Glide

I use the next code to manually add a local bitmap into Glide's cache.
Now the problem I have is that I don't know how to read it back into an ImageView.
To save it I use a key as the signature.
fun saveBitmap(context: Context, key: String, bitmap: Bitmap) {
val signature = ObjectKey(key)
GlideApp.with(context)
.asBitmap()
.signature(signature)
.load(bitmap)
.preload()
}
Not working
fun loadImage(key: String, target: ImageView) {
val signature = ObjectKey(key)
GlideApp.with(target.context)
.load(key)
.signature(signature)
.into(target)
}

Preload low resolution image with Coil before high quality

I'm trying to display a low-res image as a full tv background while loading the high quality image. With this solution I could avoid the Black-To-image blink effect.
My low-res image is loaded in a previous fragment and is stored in the cache.
I try to achieve this with Coil. I've seen there is a memoryCachePlaceholder, but I couldn't make it work with my solution. Any hint ?
requireContext().imageLoader.enqueue(ImageRequest.Builder(requireContext())
.data(myLowResUrl)
.crossfade(0)
.target(object : TransitionTarget {
override val drawable get() = showImageView!!.drawable
override val view get() = showImageView!!
override fun onSuccess(result: Drawable) {
showImageView?.setImageDrawable(result)
showImageView?.load(myHighResUrl) {
placeholderMemoryCacheKey(showImageView?.result?.request?.memoryCacheKey)
}
}
})
.build())

why is my image not showing in recyclerview?

I followed a tutorial on youtube on how to retrieve images and text from Firestore and display them in the RecyclerView. I'm using the Glide library to display captured images. I bind my image using this code:
override fun onBindViewHolder(holder: itemViewHolder, position: Int) {
val produk=listOfItem[position]
holder.kategori.text=produk.productCategory
holder.berat.text=produk.productWeight.toString()
//img
Glide.with(context)
.load(produk.productIcon)
.into(holder.gambarBarang)
}
I try to use FirebaseStorage references:
val storage = FirebaseStorage.getInstance()
val gsReference = storage.getReferenceFromUrl(produk.productIcon.toString())
Glide.with(context)
.load(produk.productIcon)
.into(holder.gambarBarang)
but that seems to make the situation even worse, i get this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.b1, PID: 8300
java.lang.IllegalArgumentException: The storage Uri could not be
parsed.
I also noticed that some of the images that I put in the imageView are not visible. Is this a result of the inserted image being too large? Anyone know how to solve it?
Edit: I try to log the produk.productIcon.toString() inside the onBindViewHolder, and get this:
D/TAG: com.google.firebase.storage.UploadTask$TaskSnapshot#7e89f3c
gggg
D/TAG: com.google.firebase.storage.UploadTask$TaskSnapshot#213e98e
gggg
D/TAG: com.google.firebase.storage.UploadTask$TaskSnapshot#df9e78f
I think the UploadTask come from the code that i use for adding the image to Firebase Storage:
val filePathAndName="product_images/"+""+timeStamp
val storageReference=FirebaseStorage.getInstance().
getReference(filePathAndName)
val uploadTask= storageReference.putFile(image_Uri!!)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val downloadUri = task.result
val item = Item(
""+firebaseAuth.uid,
timeStamp,
kategori,
berat_sampah.toDouble(),
deskripsi,
""+downloadUri,
)
FirestoreClass().addItem(this#AddProductActivity, FirestoreClass().getCurrentUserID(),item)
ClearData()
Is this code the cause of the error?
produk.productIcon is not an URL, in fact, it is a UploadTask.TaskSnapshot which is why it can neither be parsed as a storage reference nor as an image URL. While I don't yet understand where you are getting the UploadTask from(it's best if you make the changes there), I'd suggest you use getDownloadUrl to get a Task which will give you the URL you need to pass to Glide.
This is how it should look:
val storage = FirebaseStorage.getInstance()
storage.getReference(YOUR_STORAGE_PATH).getDownloadUrl()
.addOnSuccessListener(OnSuccessListener() {
// onSuccess method call
Glide.with(context)
.load(URL FROM LISTENER)
.into(holder.gambarBarang)
}
)
So based on #Abdullah Z Khan answer, i notice that the problem is with my image uri. I use task.getResult to get the uri, but it give me taskSnapShot instead.

Glide - Load a GIF as a placeholder

I'm using Glide in my Android project, and I have a RecyclerView with a list of CardViews in it. What I wanna do is to load an image for each CardView while showing a GIF loading image until the real image gets loaded. Now if it try to load the GIF as the main image, no problem:
Glide.with(context)
.load(R.raw.gif_loading_bar)
.into(imageView);
The code above works fine and the GIF gets loaded. But when I try to load another image and make the GIF a placeholder Android Studio shows an error "Expected resource of type drawable":
Glide.with(mContext)
.setDefaultRequestOptions(new RequestOptions()
.placeholder(R.raw.gif_loading_bar))
.load(imageUrl)
.into(imageView);
What should I do to fix this?
Glide does support GIF as placeholder. I recently faced this situation and solved it like below. Hope it helps someone like me who is facing this situtaion.
var builder = Glide.with(mContext).load(path).apply(options).thumbnail(Glide.with(mContext).load(R.raw.loader2))
if (listener == null) {
builder = builder.addListener(object : RequestListener<Drawable> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
//TODO; your code here
return false
}
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
//TODO; your code here
return false
}
})
} else {
builder = builder.addListener(listener)
}
builder.into(myImageView)
What it does
It load your gif as thumbnail until the image is downloaded from the source.
Inside the onResourceReady method it will give you the callback when the resource is ready. Make sure to put gif file under R.raw package and not in drawable package. Let me know if you face any issue.
Unfortunately, Glide does not support GIF placeholders, as mentioned in this Github issue, and this one.
This should do the job:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
GlideDrawableImageViewTarget imageViewTarget = new
GlideDrawableImageViewTarget(imageView);
Glide.with(context)
.asGif()
.setDefaultRequestOptions(new RequestOptions()
.placeholder(R.raw.gif_loading_bar))
.load(imageUrl)
.into(imageView);
More info here and here
Just make sure to change the getContext() as per your codes. For example in my case I am passing it as holder.img.getContext()
Glide.with(getContext()).load(item[position])
.thumbnail(Glide.with(getContext()).load(R.drawable.gif_image))
.fitCenter()
.crossFade()
.into(imageView);

Load vector drawable resource multiple times with glide issue

I am using glide 4.6.1 to load a Resource (a vector drawable). It's always the same resource but sometimes does not load properly. Looks Emily Blunt image.
The code:
fun AppCompatImageView.loadCircleImage(resId: Int, width: Int, height: Int) {
val options = RequestOptions()
.fitCenter()
.override(width, height)
.circleCrop()
Glide.with(context)
.load(AppCompatResources.getDrawable(context, resId))
.apply(options)
.into(this)
}

Categories

Resources