Hot questions for Using Glide in android drawable

Top 10 Java Open Source / Glide / android drawable

Question:

Here is my attempted code.

val imageView = ImageView(holder.itemView.context)

holder.itemView.textView.setCompoundDrawablesWithIntrinsicBounds(
        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.


Answer:

In Kotlin you can do as :

 Glide.with(context).load("url").apply(RequestOptions().fitCenter()).into(
        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)
            }
        }
    )

Question:

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

 Glide.with(context)
                .load(Urls.BASE_URI +items.get(holder.getAdapterPosition()).getUserPhotoUrl())
                .apply(requestOptions
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .skipMemoryCache(true).dontAnimate().fitCenter().circleCrop().override(100,100)
                )
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        holder.userPhoto.setImageDrawable(resource);

                    }
                });

Answer:

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(),
                                           R.drawable.icon_resource);
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");
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());


new Compressor(this).compressToFileAsFlowable(f)
                       .subscribeOn(Schedulers.io())
                       .observeOn(AndroidSchedulers.mainThread())
                       .subscribe(file -> getTheBitmapOfTheFile(file), throwable -> throwable.printStackTrace());
// remember close de FileOutput
fo.close();
}

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) {
            e.printStackTrace();
        }
}

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

Question:

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:

Glide.with(getActivity).load(imageUl)
         .asBitmap().placeholder(d)
         .into(imageView); 

Answer:

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

Glide.with(getActivity).load(imageUl)
         .asBitmap().placeholder(drawable)
         .into(imageView); 

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

Question:

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.


Answer:

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)
    output.close()
}