2013-08-01 168 views
4

我在我的项目中使用Volley库,但我遇到OutOfMemory异常问题。在我的应用程序中,我使用setImageUrl方法通过NetworkImageView从服务器下载大拇指和全尺寸图像。我使用BitmapLruCache:如何处理OutOfMemory异常

public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageLoader.ImageCache { 

    public static int getDefaultLruCacheSize() { 
     final int maxMemory = (int) (Runtime.getRuntime().maxMemory()/1024); 
     final int cacheSize = maxMemory/8; 

     return cacheSize; 
    } 

    public BitmapLruCache() { 
     this(getDefaultLruCacheSize()); 
    } 

    public BitmapLruCache(int sizeInKiloBytes) { 
     super(sizeInKiloBytes); 
    } 

    @Override 
    protected int sizeOf(String key, Bitmap value) { 
     return value.getRowBytes() * value.getHeight()/1024; 
    } 

    @Override 
    public Bitmap getBitmap(String url) { 
     return get(url); 
    } 


    @Override 
    public void putBitmap(String url, Bitmap bitmap) { 
     put(url, bitmap); 
    } 
} 

我越来越对的HTC Desire OutOfMemoryException(安卓2.2.2)。我该如何处理这个异常?我在做什么错了?

编辑

这个例外,我猴子试验中得到:

java.lang.OutOfMemoryError在 com.android.volley.toolbox.ByteArrayPool.getBuf(ByteArrayPool.java:101) 在 com.android.volley.toolbox.PoolingByteArrayOutputStream.expand(PoolingByteArrayOutputStream.java:76) 在 com.android.volley.toolbox.PoolingByteArrayOutputStream.write(PoolingByteArrayOutputStream.java:84) 在 com.android.volley.toolbox.BasicNetwork.entityToBytes(BasicNetwork.java:213) 在 com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:104) 在 com.android.volley .NetworkDispatcher.run(NetworkDispatcher.java:105)

@Sipka - 它并没有解决我的问题

@Muhammad巴巴尔 - 排球库处理所有网络/位图/缓存操作,所以我需要的解决方案修复由Volley造成的OutOfMemory异常。

+0

在清单 – Sipka

+0

尝试largeHeap =“真”,这可能是由于位图的大小,请参考这* * http://developer.android.com/training/displaying-bitmaps/load-bitmap.html*更多详细信息 –

+0

@Sipka - 编辑我的问题。 – Ziem

回答

-2

使用此代码来创建一个线程位图,这将有助于你

Bitmap bitmap = null; 
    HttpResponse response = null; 
    InputStream instream = null; 
    try { 

     File file = new File(Environment.getExternalStorageDirectory() 
       .toString(), floderName); 
     String s = file.getAbsolutePath(); 
     f = new File(s); 
     if (!f.exists()) { 
      HttpClient client = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(new URL(url[0]).toURI()); 
      response = client.execute(request); 
      if (response.getStatusLine().getStatusCode() != 200) { 
       return null; 
      } 
      // BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(
      // response.getEntity()); 
      instream = response.getEntity().getContent(); 

      OutputStream os = new FileOutputStream(f); 
      Globals.CopyStream(instream, os); 
      os.close(); 
      instream.close(); 
     } 
     FileInputStream fs = null; 
     try { 
      fs = new FileInputStream(f); 
     } catch (FileNotFoundException e) { 
      // TODO do something intelligent 
      e.printStackTrace(); 
     } 

     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inDither = false; // Disable Dithering mode 
     o2.inPurgeable = true; // Tell to gc that whether it needs free 
           // memory, the Bitmap can be cleared 
     o2.inInputShareable = true; // Which kind of reference will be used 
            // to recover the Bitmap data after 
            // being clear, when it will be used in 
            // the future 
     o2.inTempStorage = new byte[32 * 1024]; 
     o2.inSampleSize = 1; 
     bitmap = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, o2); 
     bit = bitmap; 
     // bit.compress(Bitmap.CompressFormat.JPEG, 90, null); 
     newsFeed.setBitmap(bit); 

     // Data.globelCoverIcon = bit; 

     // OutputStream os = new FileOutputStream(f); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 

    } 
public class Globals { 
private static final int JPEG_EOI_1 = 0xFF; 
private static final int JPEG_EOI_2 = 0xD9; 

public static void CopyStream(InputStream is, OutputStream os) { 
    final int buffer_size = 1024; 
    try { 
     byte[] bytes = new byte[buffer_size]; 
     for (;;) { 
      int count = is.read(bytes, 0, buffer_size); 
      if (count == -1) 
       break; 
      os.write(bytes, 0, count); 
     } 

    } catch (Exception ex) { 
     Log.e("App", ex.getMessage(), ex); 
    } 

} 

}

+0

这是如何处理内存不足异常的,请描述一下。 – Hamad

+0

我曾使用decodeFileDescriptor,不使用直接流解码。 – Ajay