我試圖從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