2013-08-01 1 views
4

J'utilise la bibliothèque Volley dans mon projet mais j'ai un problème avec OutOfMemory Exception. Dans mon application, je télécharge des pouces et des images en taille réelle à partir du serveur via NetworkImageView en utilisant la méthode setImageUrl. J'utilise BitmapLruCache:Comment faire face à l'exception 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); 
    } 
} 

Je reçois OutOfMemoryException sur HTC Desire (Android 2.2.2). Comment puis-je gérer cette exception? Quelque chose ne va pas?

Modifier

Cette exception, je suis arrivé au cours du test de singe:

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 - il ne résout pas mon problème

@Muhammad Babar - bibliothèque Volley gère tous les réseaux/opérations bitmap/cache donc j'ai besoin solution corrige l'exception OutOfMemory provoquée par Volley.

+0

dans le – Sipka

+0

manifeste essayer largeHeap = « true » cela pourrait être dû à la taille de bitmaps, s'il vous plaît se référer ce * http: //developer.android.com/training/displaying-bitmaps/load-bitmap.html* pour plus de détails –

+0

@Sipka - édité ma question. – Ziem

Répondre

-2

Utilisez ce code pour créer Bitmap dans un thread qui vous aidera à

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

comment cela traite-t-il avec l'exception de mémoire insuffisante, s'il vous plaît décrire cela. – Hamad

+0

J'ai utilisé decodeFileDescriptor qui n'utilise pas le décodage direct. – Ajay

Questions connexes