Load vector drawable resource multiple times with glide issue - android

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

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

Glide: error image is not showing while using thumbnail

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

How to properly fit image with background drawable in glide

I have to successfully load an image to this imageview and I am using glide. My biggest challenged however is I want this rectangle drawable to be in the image and the image to fit in the white rectangle drawable.
Here is my glide function
private fun loadUrl(url: String) {
Glide.with(this)
.asBitmap()
.load(url)
.fitCenter()
.centerInside()
.into(object : CustomTarget<Bitmap?>() {
override fun onResourceReady(
resource: Bitmap,
transition: Transition<in Bitmap?>?
) {
binding.imageview.setImageBitmap(resource)
binding.imageview.setBackgroundResource(R.drawable.white_rectangle)
}
override fun onLoadCleared(placeholder: Drawable?) {
}
})
}
My image view
<ImageView
android:id="#+id/imageview"
android:layout_width="48dp"
android:layout_height="48dp"
I want to achieve something like this.
I saw this one how can I advance this?
How to make border for circle cropped image in glide

How to save image from URL to local and get it's Uri?

I have the following code -
verifyOtpViewModel.userData.observe(this, Observer {
if (it == null) return#Observer
if (it.profileImage != null) {
...
}
}
profileImage is my image URL.
I need an updated way, by 2020 standard to get the image bitmap from the URL and then get the URI from that bitmap.
All answers on this subject use the soon-to-be deprecated AsyncTask and I was hoping for a better solution, maybe a 3rd party library even.
Thanks!
First download the image if it's not already downloaded
GlideApp is the generated API:
Add a class with exact same name. Be sure to Rebuild after adding this code snippet:
you need to use kapt instead of annotactionProccesser on Kotlin.
#GlideModule
class MyAppGlideModule : AppGlideModule()
diskCacheStrategy(DiskCacheStrategy.NONE) don't let Glide cache this image.
Target.Size_Original keep the original size when you download the image from url.
GlideApp
.with(context)
.asBitmap()
.load(uri)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(object : CustomTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
override fun onLoadCleared(placeholder: Drawable?) {}
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
//Save image file
saveFile(resource)
//Load into imageview if needed
}
}
})
Save the image file. It's recommended to use background threads for this.
val randomFilename = //generate a name based on your preferences
val file = File(saveDirectory, randomFilename)
val fos = FileOutputStream(file)
val bos = BufferedOutputStream(fos)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos)
bos.close()
Update your room database with the name of the file to retrieve that later.
#Query("UPDATE table_name SET image_filename = :fileName WHERE id = :id ")
fun updateImageFilename(id: Int, filename: String)

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

Categories

Resources