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 decodeSampledBitmapFromResource(int resId, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from   www . j  av a  2s. c o  m*/
    BitmapFactory.decodeResource(context.getResources(), resId, options);

    // Calculate inSampleSize
    float sampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    Log.d("----> Sample Size:", sampleSize + "");
    if (sampleSize < 1) { // return a scaled UP version of image
        Bitmap bg = BitmapFactory.decodeResource(context.getResources(), resId);
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bg, (int) (bg.getWidth() / sampleSize),
                (int) (bg.getHeight() / sampleSize), true);
        bg.recycle();
        return scaledBitmap;
    } else { // return a scaled DOWN version of image
        options.inSampleSize = Math.round(sampleSize);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(context.getResources(), resId, options);
    }
}

From source file:Main.java

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    if (bmp == null || bmp.isRecycled())
        return null;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();
    }// w  ww .  java  2  s  .co  m

    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static Bitmap rotateBitmapDegrees(Bitmap bitmap, int orientation) {
    if (orientation == 0) {
        return bitmap;
    }/*from w  ww .ja  v a 2  s .c  o m*/

    Matrix matrix = new Matrix();
    matrix.setRotate(orientation);
    Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    bitmap.recycle();
    return bmRotated;
}

From source file:Main.java

private static Bitmap rotate(Bitmap bmp, int rotation) {
    if (rotation == 0)
        return bmp;

    try {//from   w  ww  . ja  v  a  2  s. c  om
        Matrix matrix = new Matrix();
        matrix.preRotate(rotation);
        return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
    } finally {
        bmp.recycle();
    }
}

From source file:Main.java

static public Bitmap cropBitmap(Bitmap src, int x, int y, int width, int height, boolean isRecycle) {
    if (x == 0 && y == 0 && width == src.getWidth() && height == src.getHeight()) {
        return src;
    }/*from w  w  w  .j  a  v  a 2 s  .c om*/
    Bitmap dst = Bitmap.createBitmap(src, x, y, width, height);
    if (isRecycle && dst != src) {
        src.recycle();
    }
    return dst;
}

From source file:Main.java

public static Bitmap rotateBitmapDegree(Bitmap bm, int degree) {
    Bitmap returnBm;/* w  w  w .jav  a2  s.  c o  m*/
    int w = bm.getWidth();
    int h = bm.getHeight();

    Matrix matrix = new Matrix();
    matrix.postRotate(degree);

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

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

    return returnBm;
}

From source file:Main.java

public static Bitmap decodeSampledBitmap(String fileName, int reqWidth, int reqHeight) {

    final int rotation = getRotation(fileName);

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from w w w .  j  a  v  a  2  s .c  om*/
    BitmapFactory.decodeFile(fileName, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap thumb = null;
    try {
        thumb = BitmapFactory.decodeFile(fileName, options);
        return rotate(thumb, rotation);
    } finally {
        if (rotation != 0 && thumb != null) {
            thumb.recycle();
        }
    }
}

From source file:Main.java

@SuppressLint("NewApi")
public static String getBitmapStrBase64(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 100, baos);
    byte[] bytes = baos.toByteArray();
    String data = Base64.encodeToString(bytes, 0, bytes.length, Base64.DEFAULT);

    if (bitmap != null) {
        bitmap.recycle();
        bitmap = null;/*from ww w .j  a  v a 2  s  . c  om*/
    }

    return data;
}

From source file:Main.java

public static Bitmap getCircularBitmapImage(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());
    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;
    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
    if (squaredBitmap != source) {
        source.recycle();
    }/*  w  w w . ja  v  a  2 s  .c  o m*/
    Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP,
            BitmapShader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setAntiAlias(true);
    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);
    squaredBitmap.recycle();
    return bitmap;
}

From source file:Main.java

public static void prepare(Activity activity, int id, int width) {
    if (sCoverBitmap != null) {
        sCoverBitmap.recycle();//ww  w.j a  v  a2  s  .c o m
    }
    Rect rectgle = new Rect();
    Window window = activity.getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
    int statusBarHeight = rectgle.top;

    ViewGroup v1 = (ViewGroup) activity.findViewById(id).getRootView();
    v1.setDrawingCacheEnabled(true);
    Bitmap source = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);
    if (statusBarHeight != 0) {
        sCoverBitmap = Bitmap.createBitmap(source, 0, statusBarHeight, source.getWidth(),
                source.getHeight() - statusBarHeight);
        source.recycle();
    } else {
        sCoverBitmap = source;
    }
    sWidth = width;
}