Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:BitmapFactory.decodeStream OutOfMemoryException-SoftReference是解决方案吗?_Android_Exception_Out Of Memory_Soft References_Bitmapfactory - Fatal编程技术网

Android:BitmapFactory.decodeStream OutOfMemoryException-SoftReference是解决方案吗?

Android:BitmapFactory.decodeStream OutOfMemoryException-SoftReference是解决方案吗?,android,exception,out-of-memory,soft-references,bitmapfactory,Android,Exception,Out Of Memory,Soft References,Bitmapfactory,我得到一个OutOfMemory异常: 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.grap

我得到一个OutOfMemory异常:

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(尽管我在添加此代码之前遇到了错误):

//希望避免BitmapFactory.decodeStream()返回空错误的类
//      http://code.google.com/p/android/issues/detail?id=6066
静态类FlushedInputStream扩展FilterInputStream{
公共FlushedInputStream(InputStream InputStream){
超级(输入流);
}
@凌驾
公共长跳过(长n)引发IOException{
长的总重量=0升;
while(totalBytesSkipped
基本上,我有一个活动,可以下载图像,将它们放在框架布局中,然后淡入淡出框架,进行幻灯片放映。framelayout有两个imageview子级

我见过有人说SoftReference被用来防止OOMEException,但我不明白这将如何应用到我的代码中(希望如此)防止这个错误


有人能解释一下这是如何实现的吗?

我处理的问题与您相同,我发现问题在于BitmapFactory.decodeStream的内存消耗(如日志所示)。您需要做的是在调用BitmapFactory.decodeStream之前缩放位图,您可以找到一篇非常好的文章来解决您的问题


希望有帮助

在这种情况下,软引用可能对您没有帮助。您只是试图一次在内存中保存太多图像。或者,您试图下载的图像太大,解码后无法放入内存。您可以尝试使用BitmapFactory.Options.inSampleSize将图像缩小到显示大小。
// 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;
        }
    }