Monday, April 25, 2016

Android Dealing with Out of memory errors on Image load


1. When image that is having size (width and height) it gives below error
ingmobile.com.eyeome W/OpenGLRenderer Bitmap too large to be uploaded into a texture
04-21 07:08:21.080  17783-17783/livingmobile.com.eyeome W/OpenGLRenderer Bitmap too large to be uploaded into a texture
04-21 07:08:21.080  17783-17783/livingmobile.com.eyeome W/OpenGLRenderer Bitmap too large to be uploaded into a texture
04-21 07:08:21.090  17783-17783/livingmobile.com.eyeome W/OpenGLRenderer Bitmap too large to be uploaded into a texture
04-21 07:08:21.110  17783-17783/livingmobile.com.eyeome W/OpenGLRenderer Bitmap too large to be uploaded into a texture
04-21 07:08:21.130  17783-17783/livingmobile.com.eyeome W/OpenGLRenderer Bitmap too large to be uploaded into a texture

The solution for this is to resize the image 

2. Out of memory error while decoding the stream

java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:299)
at ln.SlideShowActivity.setThumbnailImageAndSave(SlideShowActivity.java:381)
at ln.SlideShowActivity$3.run(SlideShowActivity.java:255)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAn


public void setThumbnailImageAndSave(final ImageView imgView, File imgFile) {
    
    if(bitmapCache != null)
    {
        bitmapCache.recycle();
        bitmapCache = null;
    }
    /* There isn't enough memory to open up more than a couple camera photos */
    /* So pre-scale the target bitmap into which the file is decoded */
    
    /* Get the size of the ImageView */
    int targetW = imgView.getWidth();
    int targetH = imgView.getHeight();
    
    /* Get the size of the image */
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imgFile.getAbsolutePath(), bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;
    
    /* Figure out which way needs to be reduced less */
    int scaleFactor = 1;
    if ((targetW > 0) || (targetH > 0)) {
        scaleFactor = Math.min(photoW/targetW, photoH/targetH);
    }
    
    /* Set bitmap options to scale the image decode target */
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;
    
    /* Decode the JPEG file into a Bitmap */
    bitmapCache = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), bmOptions);
    
    if(bitmapCache != null) {
        /* Associate the Bitmap to the ImageView */
        imgView.setImageBitmap(bitmapCache);
        imgView.setVisibility(View.VISIBLE);
    }
}

references:

No comments:

Post a Comment