我收到了很多來自用戶的這些異常:java.lang.OutOfMemoryError:位圖大小超過了VM預算 - 但我無法在模擬器上重現問題。當從url解碼圖像時出現OutOfMemory
我用這個作爲我的ImageDownloader http://code.google.com/p/android-imagedownloader/source/browse/trunk/src/com/example/android/imagedownloader/ImageDownloader.java#185,並且在嘗試解碼圖像時出現第185行的問題。
關於這個錯誤,還有很多關於stackoverflow的其他問題,但沒有一個符合我的具體情況。在這個問題的解決方案可以工作:https://stackoverflow.com/a/823966但我似乎無法得到它的工作。
以下是我在ImageDownloader改變:
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
// return BitmapFactory.decodeStream(inputStream);
// Bug on slow connections, fixed in future release.
FlushedInputStream flushedStream = new FlushedInputStream(inputStream);
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(flushedStream, null, o);
Log.d(LOG_TAG, "decodeStream - outWidth=" + o.outWidth + " outHeight=" + o.outHeight);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of
// 2.
int scale = 1;
while (o.outWidth/scale/2 >= REQUIRED_SIZE && o.outHeight/scale/2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(flushedStream, null, o2);
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
它調用decodeStream()第二次失敗時。錯誤是SkImageDecoder :: Factory返回null。我想可能inputStream在第二次調用時關閉或不可用,所以我試圖在上面第一行使用BufferedHttpEntity,但沒有運氣。
有人可以引導我朝着正確的方向嗎?謝謝。
谷歌它有很多關於您的問題的答案:) – RobinHood 2012-02-14 08:38:30