Thursday, September 18, 2014

Android Out of Memory Errors


Frequently we encounter esp. when dealing with Images is the out of memory error ;

Here's some ideas and code snippets you can use :
Easy way out : (Not recommended)
Android:largeHeap=true in manifest 


Better techniques 


On button click:
  Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
       // request code  
       String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());  
       _file = new File(_dir, "appname"+timeStamp+".jpg");  
       cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(_file));  
       startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  



and then do this in onactivityresult:

and then again in onActivityResult do this:
 if( requestCode == CAMERA_PIC_REQUEST)  
     {  
 Intent cropIntent = new Intent("com.android.camera.action.CROP",  
             android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
         cropIntent.setDataAndType(Uri.fromFile(_file), "image/*");  
         cropIntent.putExtra("crop", "true");  
         cropIntent.putExtra("aspectX", 4);  
         cropIntent.putExtra("aspectY", 3);  
         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());  
          _file = new File(Environment.getExternalStorageDirectory(), timeStamp + ".jpg");  
         cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(_file));  
         startActivityForResult(cropIntent, PIC_CROP_REQUEST);  
 }  


checkBitmapFitsInMemory :


   public static boolean checkBitmapFitsInMemory(long bmpwidth, long bmpheight, int bitdepth) {  
     long reqsize = bmpwidth * bmpheight * (bitdepth) / 8;  
     long allocNativeHeap = Debug.getNativeHeapAllocatedSize();  
     final long heapPad = (long) Math.max(3 * 1024 * 1024, Runtime.getRuntime()  
         .maxMemory() * 0.1);  
     if ((reqsize + allocNativeHeap + heapPad) >= Runtime.getRuntime()  
         .maxMemory()) {  
       return false;  
     }  
     return true;  
   }  


 resizeBitMapImage:

 public static Bitmap resizeBitMapImage(String filePath, int targetWidth,  
     int targetHeight) {  
   Bitmap bitMapImage = null;  
   // First, get the dimensions of the image  
   Options options = new Options();  
   options.inJustDecodeBounds = true;  
   BitmapFactory.decodeFile(filePath, options);  
   double sampleSize = 0;  
   // Only scale if we need to  
   // (16384 buffer for img processing)  
   Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math  
   .abs(options.outWidth - targetWidth);  
   if (options.outHeight * options.outWidth * 2 >= 1638) {  
     // Load, scaling to smallest power of 2 that'll get it <= desired  
     // dimensions  
     sampleSize = scaleByHeight ? options.outHeight / targetHeight  
         : options.outWidth / targetWidth;  
     sampleSize = (int) Math.pow(2d,  
         Math.floor(Math.log(sampleSize) / Math.log(2d)));  
   }  
   // Do the actual decoding  
   options.inJustDecodeBounds = false;  
   options.inTempStorage = new byte[128];  
   while (true) {  
     try {  
       options.inSampleSize = (int) sampleSize;  
       bitMapImage = BitmapFactory.decodeFile(filePath, options);  
       break;  
     } catch (Exception ex) {  
       try {  
         sampleSize = sampleSize * 2;  
       } catch (Exception ex1) {  
       }  
     }  
   }  
   return bitMapImage;  
 }  

{"happy" : "coding”}!

Credits to : blrdroid group and Khalid.

No comments:

Post a Comment