Skip to content Skip to sidebar Skip to footer

Android, Out Of Memory Converting To Black And White

I'm converting some resources to black and white when user touch them. I have this function to do this in my Util java file: public static Bitmap convertColorIntoBlackAndWhiteImage

Solution 1:

this may help you...

publicstatic Bitmap convertColorIntoBlackAndWhiteImage(Bitmap orginalBitmap) {
    ColorMatrixcolorMatrix=newColorMatrix();
    colorMatrix.setSaturation(0);

    ColorMatrixColorFiltercolorMatrixFilter=newColorMatrixColorFilter(
            colorMatrix);
    BitmapblackAndWhiteBitmap= orginalBitmap.copy(
            Bitmap.Config.ARGB_8888, true);
    // Recycle the Bitmap and free the memory once you copied into new// Bitmap
    orginalBitmap.recycle();
    orginalBitmap = null;
    // still problem exists call// System.gc();// System.gc();Paintpaint=newPaint();
    paint.setColorFilter(colorMatrixFilter);

    Canvascanvas=newCanvas(blackAndWhiteBitmap);
    canvas.drawBitmap(blackAndWhiteBitmap, 0, 0, paint);

    return blackAndWhiteBitmap;
}

and it seems like you are trying to convert Image into gray color. try this...

publicstatic Bitmap toGrayBitmap(Bitmap bitmap) {
        if (bitmap == null || bitmap.isRecycled()) {
            returnnull;
        }
        finalintwidth= bitmap.getWidth();
        finalintheight= bitmap.getHeight();

        BitmapcanvasBitmap= Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        Canvascanvas=newCanvas(canvasBitmap);
        canvas.drawARGB(0, 0, 0, 0);

        Paintpaint=newPaint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.WHITE);
        ColorMatrixcolorMatrix=newColorMatrix();
        colorMatrix.setSaturation(0);
        ColorMatrixColorFilterfilter=newColorMatrixColorFilter(
                colorMatrix);
        paint.setColorFilter(filter);

        canvas.drawBitmap(bitmap, 0, 0, paint);
        bitmap.recycle(); // do it if still problem existsreturn canvasBitmap;
    }

Solution 2:

While working with Bitmaps you should care about memory consumption (specially for devices with large screen and high resolution).

Create a bitmap and recycle that every time you want to convert image.

From android docs:

Bitmaps take up a lot of memory, especially for rich images like photographs. For example, the camera on the Galaxy Nexus takes photos up to 2592x1936 pixels (5 megapixels). If the bitmap configuration used is ARGB_8888 (the default from the Android 2.3 onward) then loading this image into memory takes about 19MB of memory (2592*1936*4 bytes), immediately exhausting the per-app limit on some devices.

Read this:

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Post a Comment for "Android, Out Of Memory Converting To Black And White"