title: Android OutOfMemory converting image byte array into Drawable
body: <p>I am developing chatting application which connects with Facebook and Gatlk, fetch contacts and VCard using ASMACK API and showing them in ListView.</p>
  
  <p>Getting contact images from VCard which is in BYTE ARRAY. I need to convert this byte array into image(Bitmap or Drawable), but getting OutOfMemory exception while creating Drawable from image byte array and also while scrolling ListView it occurs.</p>
  
  <p>Below is the code snippet,  also tried by converting it into Bitmap, but with Bitmap outofmemory exception is coming more frequently. Tried with bitmap.recyle() method as solution provided at forum for such issue. But using recycle() most of places getting exception something like “using recycled images”.</p>
  
  <p>Here if the LOG:</p>
  
  <pre><code>java.lang.OutOfMemoryError: bitmap size exceeds VM budget
   E/dalvikvm-heap(26048): 10000-byte external allocation too large for this process.
   E/GraphicsJNI(26048): VM won't let us allocate 10000 bytes
  </code></pre>
  
  <p>Please let me know the best approach to convert bytearray in Bitmap or Drawable.
  Code snippet is here:</p>
  
  <pre><code>public static Drawable createDrawableImageFromByteArray(Context context, byte[] imageByteArray){
      Drawable drawable = null;
      try{
          if(imageByteArray != null){                                                                                                 
              BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
              bmpFactoryOptions.inJustDecodeBounds = true;                                                            
              int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)55);
              int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)55);
  
              if (heightRatio &gt; 1 || widthRatio &gt; 1){
                  if (heightRatio &gt; widthRatio){
                      bmpFactoryOptions.inSampleSize = heightRatio;  
                  } else {
                      bmpFactoryOptions.inSampleSize = widthRatio;   
                  }   
              }                                              
              bmpFactoryOptions.inJustDecodeBounds = false;
              bmpFactoryOptions.inPurgeable = true;
              bmpFactoryOptions.inInputShareable = true;
              drawable = Drawable.createFromResourceStream(context.getResources(), new TypedValue(), new ByteArrayInputStream(imageByteArray), "testimg", bmpFactoryOptions);
          }
      }catch(OutOfMemoryError e){
          Utils.debugLog("****** createBitmaException :: " + e);
      }catch(Exception e){
          Utils.debugLog("****** createBitmaException :: " + e);
      }
      return drawable;                             
  }
  </code></pre>
  
  <p>Thank you</p>