简体   繁体   中英

How to compress image in Glide on Android

I want show image in my application and for show images I use Glide library.
In my codes I can change image size but I want compress images.

Glide.with(context)
        .load(model.get(position).getImageUrl2())
        .placeholder(R.drawable.default_image)
        .override(900, 600)
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model,
                                           Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                return false;
            }
        })
        .into(holder.newsImage);

How can I compress images?

Please help me

Get your image

Drawable drawable = getDrawable(R.drawable.bird);

Get the bitmap from drawable object

Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

Initialize a new ByteArrayStream

ByteArrayOutputStream stream = new ByteArrayOutputStream();

Compress the bitmap with JPEG format and quality 50%

bitmap.compress(Bitmap.CompressFormat.JPEG,50,stream);
byte[] byteArray = stream.toByteArray();
Bitmap compressedBitmap = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);

Use the compressed Bitmap to load image. I know its not what you want but i think it might helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM