Thursday, September 3, 2015

[Android][Resolved] Bitmap too large to be uploaded into a texture

To stop the error message, the fastest method is try add 2 line of code in Androidmanifest.xml
<application
        ...
        android:hardwareAccelerated="false"
        android:largeHeap="true"
>

Setting android:largeHeap to true is to increase the mobile device memory which is allocated to the application. However, it’s is not always a good idea to use android:largeHeap="true" because using the extra memory will increasingly be to the detriment of the overall user experience because garbage collection will take longer and system performance may be slower when task switching or performing other common operations. You should try to fix to run the reason cause the app out of memory.

Resize bitmap

Scale the bitmap first before you display on the screen to save the memory, I used an BitmapLoader class from internet to do that:
package com.example.android.resize.example.widgets;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


/**
 * Class BitmapLoader is class for handling bitmap related feature.
 * Feature: Resize image
 */
public class BitmapLoader {
    public static int getScale(int originalWidth, int originalHeight, final int requiredWidth,final int requiredHeight){
        //a scale of 1 means the original dimensions  of the image are maintained
        int scale=1;
        //calculate scale only if the height or width of the image exceeds the required value.
        if((originalWidth>requiredWidth) || (originalHeight>requiredHeight)) {
            //calculate scale with respect to the smaller dimension
            if(originalWidth<originalHeight) scale= Math.round((float) originalWidth / requiredWidth);
            else scale= Math.round((float) originalHeight / requiredHeight);
        }
        return scale;
    }

    public static BitmapFactory.Options getOptions(String filePath,int requiredWidth,int requiredHeight) {
        BitmapFactory.Options options=new BitmapFactory.Options();
        //setting inJustDecodeBounds to true , ensures that we are able to measure, the dimensions of the image,without actually allocating it memory
        options.inJustDecodeBounds=true;
        //decode the file for measurement
        BitmapFactory.decodeFile(filePath, options);
        //obtain the inSampleSize for loading a scaled down version of the image. options.outWidth and options.outHeight are the measured dimensions of the original image
        options.inSampleSize=getScale(options.outWidth,options.outHeight, requiredWidth, requiredHeight);
        //set inJustDecodeBounds to false again so that we can now actually allocate the bitmap some memory
        options.inJustDecodeBounds=false;
        return options;
    }

    public static Bitmap loadBitmap(String filePath,int requiredWidth,int requiredHeight){
        BitmapFactory.Options options= getOptions(filePath, requiredWidth, requiredHeight);
        return BitmapFactory.decodeFile(filePath, options);
    }

    public static Bitmap resizeBitmap(Bitmap bitmap, int newWidth, int newHeight) {
        //Bitmap bitmap;
        int height = (bitmap.getHeight() * newHeight / bitmap.getWidth());
        Bitmap scale = Bitmap.createScaledBitmap(bitmap, newHeight, height, true);
        return scale;
    }
}
Do resize:
bitmap = BitmapLoader.resizeBitmap(sourceBitmap, saveImageWidth,saveImageHeight);
Of course you can try others method:
http://stackoverflow.com/questions/3331527/android-resize-a-large-bitmap-file-to-scaled-output-file

Recycle bitmap

Using recycle() to recycle the bitmap if you think you won’t use it anymore, it’s for free the native object associated with this bitmap, and clear the reference to the pixel data.
http://developer.android.com/reference/android/graphics/Bitmap.html

And this is my example:
Bitmap bitmap = BitmapLoader.loadBitmap(imageDir.getEncodedPath(), width, height);
imageView.setImageBitmap(bitmap);
bitmap.recycle();
To know more examples, you can visit this link:
http://www.codota.com/android/methods/android.graphics.Bitmap/recycle

Save and load bitmap from cache

The method was provided by android official site and made detail explained:
https://developer.android.com/training/displaying-bitmaps/manage-memory.html



Reference:
http://android-er.blogspot.hk/2013/08/handle-error-of-outofmemoryerror-and.html
http://stackoverflow.com/questions/27396892/androidlargeheap-true-in-mainifast-advantages
http://stackoverflow.com/questions/27396892/androidlargeheap-true-in-mainifast-advantages

Future reading about memory allocation in android
https://developer.android.com/training/articles/memory.html

No comments :

Post a Comment