Question

I have a function that take a bitmap, two colors and return a BitmapDrawable :

// Theme function
    static public BitmapDrawable pFilter(Bitmap bitmap, int backgroundColor, int foregroundColor)
{

    Bitmap bitmapCopy = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), null, true);

    int[] pixels = new int[bitmapCopy.getByteCount()]; 

    bitmapCopy.getPixels(pixels, 0, bitmapCopy.getWidth(), 0, 0, bitmapCopy.getWidth(), bitmapCopy.getHeight());

    // Call native function

    bitmapCopy.setPixels(pixels, 0, bitmapCopy.getWidth(), 0, 0, bitmapCopy.getWidth(), bitmapCopy.getHeight());

    BitmapDrawable finalDrawable = new BitmapDrawable(Application.getAppContext().getResources(), bitmapCopy);

    return finalDrawable;
}

// Custom Imageview
public class CustomImageView extends ImageView 
{

private BitmapDrawable sourceImage;

private CustomTheme theme;

// [...]

private void refreshImageView()
{

  super.setImageDrawable(theme.pFilter(sourceImage.getBitmap(), theme.backgroundColor, theme.foregroundColor));

}

My problem is that after about 80 calls to this function (with 10px*10px bitmaps), I get a OutOfMemory exception on this line :

int[] pixels = new int[bitmapCopy.getByteCount()]; 

Thanks.

Was it helpful?

Solution 2

The error was in my JNI call, not in Android :

(*env)->ReleaseIntArrayElements(env, pixels, nativePixels, JNI_COMMIT);

JNI_COMMIT : copy back the content but do not free the elems buffer

The solution is to use 0 instead of JNI_COMMIT :

(*env)->ReleaseIntArrayElements(env, pixels, nativePixels, 0);

OTHER TIPS

after calling the pFilter(??); call bitmap.recycle(); on the orignal bitmap

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top