Android的图像大小调整 [英] Android image resize
问题描述
我有存储在SD卡上(每个〜4MB的大小)的图像。
I have images stored on sd card(size of each ~ 4MB).
我要调整每次,比它设置为ImageView的。
I want to resize each, than set it to ImageView.
但是,使用 BitmapFactory.de codeFILE(路径)
becouse异常结果我不能这样做 java.lang.OutOfMemoryError
时,会出现。
But I cannot do it using BitmapFactory.decodeFile(path)
becouse Exception
java.lang.OutOfMemoryError
is appeared.
我如何可以调整图像而不在内存中加载它。是真的吗?
How can i resize image without loading it in the memory. Is it real?
推荐答案
使用位图选项:
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
然后设置在德code选项
Then set the options in the decode
BitmapFactory.decodeFile(path, options)
您甚至可以写这样的方法在您需要的分辨率来获得尺寸图像。
You could even write a method like this to obtain a sized image at your desired resolution.
以下方法检查大小的图像,然后去codeS从文件中,样品尺寸从SD卡相应调整你的形象,同时保持内存使用率处于较低。
The following method checks the size the image, then decodes from file, with in-sample size to resize your image from sdcard accordingly, while keeping memory usage at a low.
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);
}
这篇关于Android的图像大小调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!