Example usage for android.graphics Bitmap recycle

List of usage examples for android.graphics Bitmap recycle

Introduction

In this page you can find the example usage for android.graphics Bitmap recycle.

Prototype

public void recycle() 

Source Link

Document

Free the native object associated with this bitmap, and clear the reference to the pixel data.

Usage

From source file:Main.java

public static Bitmap rotateBitmap(Bitmap b, int degrees) {
    if (degrees != 0 && b != null && !b.isRecycled()) {
        Matrix m = new Matrix();
        m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
        try {//from w  w w  . j  a v  a 2s  .  c  o m
            Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
            if (b != b2) {
                b.recycle();
            }
            b = b2;
        } catch (OutOfMemoryError ex) {
        }
    }
    return b;
}

From source file:Main.java

public synchronized static Bitmap getRotatedBitmap(Bitmap bitmap, int degrees) {
    if (degrees != 0 && bitmap != null) {
        Matrix m = new Matrix();
        m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
        try {/*from ww w  .j  a v a 2  s.com*/
            Bitmap b2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
            if (bitmap != b2) {
                bitmap.recycle();
                bitmap = b2;
            }
        } catch (OutOfMemoryError e) {
            // Log.d(PhotoUtil.class.getSimpleName(),
            // "Error: "+e.getMessage());
        }
    }

    return bitmap;
}

From source file:Main.java

public static byte[] bitmap2Bytes(Bitmap bitmap, Bitmap.CompressFormat mCompressFormat,
        final boolean needRecycle) {
    byte[] result = null;
    ByteArrayOutputStream output = null;
    try {//from  w  w  w  . j  av a2s  .  c  o  m
        output = new ByteArrayOutputStream();
        bitmap.compress(mCompressFormat, 100, output);
        result = output.toByteArray();
        if (needRecycle) {
            bitmap.recycle();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:Main.java

public static Bitmap imageWithFixedRotation(Bitmap bm, int degrees) {
    if (bm == null || bm.isRecycled())
        return null;

    if (degrees == 0)
        return bm;

    final Matrix matrix = new Matrix();
    matrix.postRotate(degrees);/*w ww  . j  av  a2  s  .  co m*/
    Bitmap result = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
    if (result != bm)
        bm.recycle();
    return result;

}

From source file:Main.java

public static Bitmap fixRotateMirrorImage(Bitmap src) {
    Matrix rotateRight = new Matrix();
    float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1 };
    Matrix matrixMirrorY = new Matrix();
    matrixMirrorY.setValues(mirrorY);/*w  w w .  ja va2s  .c o m*/
    rotateRight.postConcat(matrixMirrorY);
    rotateRight.preRotate(270);
    final Bitmap rotMirrorImg = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), rotateRight,
            true);
    src.recycle();

    return rotMirrorImg;
}

From source file:Main.java

public static Bitmap rotate(Bitmap b, int degrees, Matrix m) {
    if (degrees != 0 && b != null) {
        if (m == null) {
            m = new Matrix();
        }/*  w  ww .  j  a va 2 s . c  om*/
        m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
        try {
            Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
            if (b != b2) {
                b.recycle();
                b = b2;
            }
        } catch (OutOfMemoryError ex) {
            // We have no memory to rotate. Return the original bitmap.
            Log.e(TAG, "Got oom exception ", ex);
        }
    }
    return b;
}

From source file:com.bobomee.android.common.util.ScreenUtil.java

/**
 * http://stackoverflow.com/questions/12742343/android-get-screenshot-of-all-listview-items
 *//*from  w  ww .j  a v  a 2 s.c  om*/
public static Bitmap shotListView(ListView listview) {

    ListAdapter adapter = listview.getAdapter();
    int itemscount = adapter.getCount();
    int allitemsheight = 0;
    List<Bitmap> bmps = new ArrayList<Bitmap>();

    for (int i = 0; i < itemscount; i++) {

        View childView = adapter.getView(i, null, listview);
        childView.measure(View.MeasureSpec.makeMeasureSpec(listview.getWidth(), View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
        childView.setDrawingCacheEnabled(true);
        childView.buildDrawingCache();
        bmps.add(childView.getDrawingCache());
        allitemsheight += childView.getMeasuredHeight();
    }

    Bitmap bigbitmap = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight,
            Bitmap.Config.ARGB_8888);
    Canvas bigcanvas = new Canvas(bigbitmap);

    Paint paint = new Paint();
    int iHeight = 0;

    for (int i = 0; i < bmps.size(); i++) {
        Bitmap bmp = bmps.get(i);
        bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
        iHeight += bmp.getHeight();

        bmp.recycle();
        bmp = null;
    }

    return bigbitmap;
}

From source file:Main.java

public static Bitmap getRotatedBitmap(Bitmap bitmap, int degrees) {
    if (degrees != 0 && bitmap != null) {
        Matrix m = new Matrix();
        m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
        try {/*from  w w  w .  j  av a2 s  .  c  o  m*/
            Bitmap b2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
            if (bitmap != b2) {
                bitmap.recycle();
                bitmap = b2;
            }
        } catch (OutOfMemoryError ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        }
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap int2Icon(Context context, int i) {
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), 0x7f020047);
    Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
            android.graphics.Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap1);
    Paint paint = new Paint();
    paint.setDither(true);//from w ww .  j a  v  a  2s  . com
    paint.setFilterBitmap(true);
    canvas.drawBitmap(bitmap, 0.0F, 0.0F, paint);
    bitmap.recycle();
    Paint paint1 = new Paint(257);
    paint1.setColor(-1);
    paint1.setTypeface(Typeface.DEFAULT_BOLD);
    paint1.setTextAlign(android.graphics.Paint.Align.CENTER);
    canvas.drawText(String.valueOf(i), bitmap.getWidth() / 2, 3 + bitmap.getHeight() / 2, paint1);
    return bitmap1;
}

From source file:Main.java

public static Bitmap rotateBitmap(Bitmap bm, int direction) {
    Bitmap returnBm;//ww w  .j  a v a  2  s . c  o m
    int w = bm.getWidth();
    int h = bm.getHeight();

    Matrix matrix = new Matrix();
    if (direction == ROTATE_LEFT) {
        matrix.postRotate(-90);
    } else if (direction == ROTATE_RIGHT) {
        matrix.postRotate(90);
    }

    returnBm = Bitmap.createBitmap(bm, 0, 0, w, h, matrix, true);

    if (bm != returnBm) {
        bm.recycle();
    }

    return returnBm;
}