我得到一個OutOfMemoryException:Android:BitmapFactory.decodeStream OutOfMemoryException - 是SoftReference的解決方案嗎?
E/AndroidRuntime(3013): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
E/AndroidRuntime(3013): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
E/AndroidRuntime(3013): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:375)
E/AndroidRuntime(3013): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:394)
在下面的功能:
static Bitmap downloadBitmap(String url)
{
final HttpClient client = new DefaultHttpClient();
final HttpGet getRequest = new HttpGet(url);
try
{
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
// Check HTTP Status code
if (statusCode != HttpStatus.SC_OK)
{
debugPrint("ImageDownloader StatusCode Error " + statusCode + " while retrieving bitmap from " + url);
return null;
}
else;
final HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream inputStream = null;
try
{
inputStream = entity.getContent();
if(inputStream == null)
{
debugPrint("ImageDownloader::downloadBitmap() - INPUTSTREAM = NULL!!!!!!");
}
else;
// THIS LINE IS GIVING THE ERROR
final Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
if(bitmap == null)
{
debugPrint("LocrPhoto::downloadBitmap() - about to return BITMAP =NULL!!!!!!");
}
else;
return bitmap;
}
catch (Exception e)
{
// Could provide a more explicit error message for IOException or IllegalStateException
getRequest.abort();
debugPrint("LocrPhoto::downloadBitmap() Error while decoding bitmap from " + url + "\n"+ e.toString());
}
finally
{
if (inputStream != null)
{
inputStream.close();
}
entity.consumeContent();
}
}
else
{
debugPrint("LocrPhoto::downloadBitmap("+url+") - entity = NULL!!!!!");
}
}
catch (Exception e)
{
// Could provide a more explicit error message for IOException or IllegalStateException
//getRequest.abort();
debugPrint("LocrPhoto::downloadBitmap() Error while retrieving bitmap from " + url + "\n"+ e.toString());
}
finally
{
if (client != null)
{
// CLOSE CONNECTION
}
}
debugPrint("LocrPhoto::downloadBitmap("+url+") - returning NULL at end of function");
return null;
}
FlushedInputStream(雖然我加入這個代碼之前得到了錯誤):
// A Class to hopefully avoid the BitmapFactory.decodeStream() returning null bug
// http://code.google.com/p/android/issues/detail?id=6066
static class FlushedInputStream extends FilterInputStream {
public FlushedInputStream(InputStream inputStream) {
super(inputStream);
}
@Override
public long skip(long n) throws IOException {
long totalBytesSkipped = 0L;
while (totalBytesSkipped < n) {
long bytesSkipped = in.skip(n - totalBytesSkipped);
if (bytesSkipped == 0L) {
int byt = read();
if (byt < 0) {
break; // we reached EOF
} else {
bytesSkipped = 1; // we read one byte
}
}
totalBytesSkipped += bytesSkipped;
}
return totalBytesSkipped;
}
}
基本上,我有一個下載圖像的活動,將它們放置在一個框架佈局中,並將幀放入和放出以進行幻燈片放映。 framelayout有兩個imageview兒童。
我見過有人說SoftReference被用來防止OOMExceptions,但我不明白這將如何應用到我的代碼(希望),以防止此錯誤。
任何人都可以解釋這可能是如何實現的嗎?
在此先感謝! r3mo
在這種情況下,SoftReferences可能無法幫助您。你只是試圖將太多的圖像一次保存在內存中。 – 2011-01-29 01:32:53