简体   繁体   中英

How Reduce image size loaded with glide on memory in android

I'm trying to load an image from the internet using glide, but the size is too big for my application(The image size in dimensions 400X400 and the memory allocating is 352KB and the size of ImageView is same as the image dimensions), I have tried to decode the bitmap after loading it and then apply it to the ImageView but it not working, anyone can help me with this problem.

this is my Glide code:

Glide.with(this)
                .asBitmap()
                .load(url)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        int byteCount = resource.getAllocationByteCount();

                        int sizeInKB = (resource.getRowBytes() * resource.getHeight()) / 1024;
                        int sizeInMB = sizeInKB / 1024;

                        Glide.with(TestActivity.this)
                                .asBitmap()
                                .load(decodeSampledBitmapFromResource(resource, 400, 400))
                                .into(ss2);

                        sizeText.setText(sizeInKB + " KB");
                    }
                });

and this is the code for decoding the bitmap:

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) >= reqHeight
                && (halfWidth / inSampleSize) >= reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

public static Bitmap decodeSampledBitmapFromResource(Bitmap bitmap, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    ByteArrayOutputStream blob = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 0, blob);
    byte[] bitmapData = blob.toByteArray();
    BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    bitmap.compress(Bitmap.CompressFormat.PNG, 0, blob);
    byte[] bitmapData2 = blob.toByteArray();
    return BitmapFactory.decodeByteArray(bitmapData2, 0, bitmapData2.length, options);
}

The size of an image depends of many things, one of them is it dimension and the quality; by default the quality format in Glide V4 is ARGB_8888 so if you are using it you can change it to RGB_565 that is smaller. You can found this and more information here .

在 Picasso 中,您可以使用 .fit() 来完成,在 Glide 中,您可以使用centerCrop()fitCenter()这些方法将图像大小调整为关联的容器。

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