gpt4 book ai didi

java - 安卓图片调整大小

转载 作者:搜寻专家 更新时间:2023-11-01 08:08:49 24 4
gpt4 key购买 nike

我将图片存储在 SD 卡上(每张约 4MB)。

我想调整每个的大小,而不是将其设置为 ImageView。

但我不能使用 BitmapFactory.decodeFile(path) 因为异常
java.lang.OutOfMemoryError 出现。

如何在不将图像加载到内存中的情况下调整图像大小。是真的吗?

最佳答案

使用位图选项:

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565; //Use this if you dont require Alpha channel
options.inSampleSize = 4; // The higher, the smaller the image size and resolution read in

然后在解码中设置选项

BitmapFactory.decodeFile(path, options)

Here is a good link to read through, about how to display Bitmaps effciently.

您甚至可以编写这样的方法来获得所需分辨率的大小图像。

以下方法检查图像的大小,然后从文件中解码,使用样本大小相应地调整来自 SD 卡的图像的大小,同时保持较低的内存使用量。

   public static Bitmap decodeSampledBitmapFromFile(string path,
int reqWidth, int reqHeight) { // BEST QUALITY MATCH

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

// Calculate inSampleSize
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
options.inPreferredConfig = Bitmap.Config.RGB_565;
int inSampleSize = 1;

if (height > reqHeight) {
inSampleSize = Math.round((float)height / (float)reqHeight);
}

int expectedWidth = width / inSampleSize;

if (expectedWidth > reqWidth) {
//if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
inSampleSize = Math.round((float)width / (float)reqWidth);
}


options.inSampleSize = inSampleSize;

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

return BitmapFactory.decodeFile(path, options);
}

关于java - 安卓图片调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11831154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com