Hot questions for Using Glide in android drawable

Top 10 Java Open Source / Glide / android drawable


Here is my attempted code.

val imageView = ImageView(holder.itemView.context)

        GlideApp.with(holder.itemView.context).load(URL).into(imageView), 0, 0, 0)

I am also not sure about Glide's image target. I have it set as .into(imageView). Since I want to add an image with TextView, there is no ImageView initialized to load the URL image into. My thought was to create one programmatically with

val imageView = ImageView(holder.itemView.context)

An error that I see is that GlideApp.with(holder.itemView.context).load(URL).into(imageView) is not being detected as a drawable.


In Kotlin you can do as :

        object : CustomTarget<Drawable>(50,50){
            override fun onLoadCleared(placeholder: Drawable?) {
                textView.setCompoundDrawablesWithIntrinsicBounds(placeholder, null, null, null)

            override fun onResourceReady(
                resource: Drawable,
                transition: Transition<in Drawable>?
            ) {
                textView.setCompoundDrawablesWithIntrinsicBounds(resource, null, null, null)


i have this code here and i want to compress drawable result how can i do that?

                .load(Urls.BASE_URI +items.get(holder.getAdapterPosition()).getUserPhotoUrl())
                .into(new SimpleTarget<Drawable>() {
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {



try this

then write this code to get the drawable bitmap then convert the bitmap to file

public void doTheJob(){
Bitmap bitmap= BitmapFactory.decodeResource(context.getResources(),
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "test.jpg");
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);

new Compressor(this).compressToFileAsFlowable(f)
                       .subscribe(file -> getTheBitmapOfTheFile(file), throwable -> throwable.printStackTrace());
// remember close de FileOutput

public void getTheBitmapOfTheFile(File file){
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        try {
            bitmap = BitmapFactory.decodeStream(new FileInputStream(file), null, options);
        } catch (FileNotFoundException e) {

but please don't forget to the permission of reading and writing to external storage


I'm using Glide to load image from url and need to have a placeholder which is generated with a transparent pattern stored as a resource and a random background color. Glide allows using Drawable as a placeholder. I have a transparent drawable resource int resId = R.drawable.placeholder; and have Color randomColor = generateRandomColor(); How can I create a Drawable which is created in combination of resource and background random color, smth. like Drawable d = resId + randomColor; ? To have as a result:



You can use the method setColorFilter of Drawable

So the code will be like:

Drawable drawable = getDrawable(resourceId);
drawable.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);


PorterDuff has a lot of modes, choose the one that's right for you


I am trying to make an image keyboard with the Android Commit Content API. In order to implement this, I need be able to store .png and .gif files on the device and create file objects from them.

Currently, I am using the Glide library to fetch images from the internet. An example image I might fetch is this. The Glide library provides me with drawables that contain animated GIFs. I need to be able to encode these drawables into .gif files that can be saved as files on the user's phone.

How can I do this?

I've looked at existing posts, such as this: How to convert a Drawable to a Bitmap?, but they aren't helpful because the Bitmap object cannot store animated GIFs.

This post: saving gif file from drawable into phone sd? and remove it from sd after usage also seems relevant, but, it doesn't explain how to turn an animated Drawable object into a list of Bitmap objects.

Also, it doesn't explain how to save a series of Bitmap objects as 1 GIF file.


This is how you can save a GifDrawable from the Glide library as a file.

private fun gifDrawableToFile(gifDrawable: GifDrawable, gifFile: File) {
    val byteBuffer = gifDrawable.buffer
    val output = FileOutputStream(gifFile)
    val bytes = ByteArray(byteBuffer.capacity())
    (byteBuffer.duplicate().clear() as ByteBuffer).get(bytes)
    output.write(bytes, 0, bytes.size)