我收到了內存不足的錯誤:內存不足錯誤 - BitmapFactory.decodeStream
下面是我的代碼:
public static Bitmap decodeSampledBitmapFromResource(InputStream inputStream,int reqWidth, int reqHeight) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
try {
while ((len = inputStream.read(buffer)) > -1) {
baos.write(buffer, 0, len);
}
baos.flush();
InputStream is1 = new ByteArrayInputStream(baos.toByteArray());
InputStream is2 = new ByteArrayInputStream(baos.toByteArray());
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither=false;
options.inPurgeable=true;
options.inInputShareable=true;
options.inJustDecodeBounds = true;
// BitmapFactory.decodeResource(res, resId, options);
BitmapFactory.decodeStream(is1,null,options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(is2,null,options); // error at this line
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
我在這行
BitmapFactory.decodeStream(is2,null,options);
我得到錯誤得到這個錯誤Out of memory on a 3250016-byte allocation.
我看過很多文章,但仍無法找到soluti在這種情況下。
你得到了什麼錯誤?請粘貼LogCat或StackTrace。 – 2013-03-06 12:41:24
爲什麼您首先將流加載到內存?從哪裏獲取圖像從服務器或從本地文件? – Triode 2013-03-06 12:41:30
我有文件夾調用數據我從那使用類加載器,它返回我一個輸入流imges,我傳遞給此方法。 – Goofy 2013-03-06 12:43:41