Я пытаюсь загрузить изображение из inte rnet, используя glide, но размер слишком велик для моего приложения (Размер изображения в размерах 400X400 и объем выделяемой памяти составляет 352 КБ, а размер ImageView такой же, как размеры изображения), я попытался декодировать растровое изображение после загрузки, а затем применить его к ImageView, но он не работает, любой может помочь мне с этой проблемой.
это мой код Glide:
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");
}
});
и это код для декодирования растрового изображения:
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);
}