我试图从SD卡读取的图像,并将它们保存在调整为1024个像素我的自定义目录,但总是我得到一个内存不足。我试过最的例子,我发现这里的计算器有关“内存不足”的工作人员......读从SD卡的图像巨大并保存调整大小(内存!)
我问我拥有Galeria应用程序如何4000个像素,从而轻松地管理图像???
谢谢。 David。
我试图从SD卡读取的图像,并将它们保存在调整为1024个像素我的自定义目录,但总是我得到一个内存不足。我试过最的例子,我发现这里的计算器有关“内存不足”的工作人员......读从SD卡的图像巨大并保存调整大小(内存!)
我问我拥有Galeria应用程序如何4000个像素,从而轻松地管理图像???
谢谢。 David。
我问我GaleríaApp如何管理图像4000像素,所以 容易?
它使用BitmapFactory.Options.inSampleSize
结合BitmapFactory
的解码方法从磁盘加载大小缩小的缩略图。它还可以平铺图像,并且只在图像的某个部分放大时才加载。
请试试以下代码。希望这会有所帮助。
位图BMAP = BitmapFactory.decodeFile(photoPath);
int orig_width = bMap.getWidth();
int orig_height = bMap.getHeight();
int aspect = orig_width/orig_height;
float aspectRatio = orig_width/orig_height;
int new_height = (int) (orig_height/(aspectRatio));
int new_width = (int) ((orig_width * aspectRatio)/2);
Bitmap scaled = Bitmap.createScaledBitmap(bMap, new_height, new_width, true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
scaled.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 5;
File sdImageMainDirectory = Environment.getExternalStorageDirectory();
FileOutputStream fileOutputStream = null;
String tempFile = "tempImage";
int quality = 50;
Bitmap myImage = BitmapFactory.decodeByteArray(bitmapdata, 0,bitmapdata.length);
try {
fileOutputStream = new FileOutputStream(sdImageMainDirectory.toString() +"/" + tempFile + ".jpg");
BufferedOutputStream bosBufferedOutputStream = new BufferedOutputStream(fileOutputStream);
myImage.compress(CompressFormat.JPEG, quality, bosBufferedOutputStream);
bosBufferedOutputStream.flush();
bosBufferedOutputStream.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
在Android上,移动应用程序在内存的限制,所以没有人会想到加载如此庞大Bitmap
像(4000px)到内存。 只是一些小技巧来处理这种情况:
Bitmap
图像。getPixel()
和setPixel
太多,这会导致非常非常糟糕的表现。改为使用getPixels()
和setPixels()
。Bitmap
,recycle()
它来释放内存(GC知道在这一点上做的),完成后。Bitmap
对象的引用,你会在以后自杀!对不起,我正在编程蜂窝。 – user991360
发送:
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
int REQUIRED_SIZE = 640;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while(true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2);
ByteArrayOutputStream bs2 = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, bs2);
getIntent().putExtra("byte_picture", bs2.toByteArray());
recive:
Bitmap photo = BitmapFactory.decodeByteArray(data.getByteArrayExtra("byte_picture"),0,data.getByteArrayExtra("byte_picture").length);
嗨alextsc,我也试过inSampleSize,但即使有一个8我得到了一个内存不足。我正在尝试读取4000像素的图像(约2Mb)。我知道我太多了,但Galerry很容易管理它们...... 无论如何感谢。 – user991360
你能发布你的代码吗? 4000x4000像素,样本大小为8,应该是加载的500x500像素图像,这在内存中略低于1 MB。这应该足够小。 – 2011-10-12 12:00:57
这是我的代码: BitmapFactory.Options界限=新BitmapFactory.Options(); bounds.inSampleSize = 8; 位图b = BitmapFactory.decodeFile(photo.getPath(),bounds); 我得到了一个内存不足,然后decodeFile被称为... – user991360