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 Drawable load_drawable_from_uri_string(Context context, String uri, int sizeX, int sizeY) {
    Drawable drawable = null;/*from   w  w w . ja  v a2  s .c o m*/
    Uri img_uri = Uri.parse(uri);
    if (uri != null) {
        try {

            InputStream inputStream = context.getContentResolver().openInputStream(img_uri);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            Bitmap small_bitmap = Bitmap.createScaledBitmap(bitmap, sizeX, sizeY, false);
            bitmap.recycle();
            drawable = new BitmapDrawable(context.getResources(), small_bitmap);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    return drawable;
}

From source file:Main.java

public static void save(Bitmap bitmap, String filepath) {
    try {/*from www.j a v  a 2s .  c o m*/
        FileOutputStream fos = new FileOutputStream(filepath);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        bitmap.recycle();
        fos.close();
    } catch (FileNotFoundException e) {

    } catch (IOException e) {

    }
}

From source file:Main.java

public static Bitmap scale(Bitmap bitmap, int width, int height, boolean recycle) throws OutOfMemoryError {
    Bitmap output = Bitmap.createScaledBitmap(bitmap, width, height, true);
    if (recycle && bitmap != output) {
        bitmap.recycle();
    }/*w ww .j  a v  a 2 s  . c  o m*/
    return output;
}

From source file:Main.java

private static Bitmap rotateBitmap(Bitmap bmp, int degrees) {
    Matrix matrix = new Matrix();
    matrix.postRotate(degrees);/*from w w w. j  ava2s  .  c om*/

    Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
    bmp.recycle();

    return newBmp;
}

From source file:Main.java

public static boolean save(Bitmap bitmap, String filepath) {
    try {/*from www  . j a v a 2 s.c  o m*/
        FileOutputStream fos = new FileOutputStream(filepath);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        bitmap.recycle();
        fos.close();
        return true;
    } catch (FileNotFoundException e) {

    } catch (IOException e) {

    }
    return false;
}

From source file:Main.java

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();
    }/* w  ww.ja  va  2s .  c om*/
    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

private static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();
    }/*from   w  ww. j  av  a  2s . c  om*/

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

    return result;
}

From source file:Main.java

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();
    }/*from ww  w. j a v a2 s.  c o  m*/

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

    return result;
}

From source file:Main.java

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 80, output);
    if (needRecycle) {
        bmp.recycle();
    }/*from w ww  . j av  a2  s.c  om*/
    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static byte[] Bitmap2Bytes(Bitmap bm, int ratio) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, ratio, baos);
    //add/* w ww .j a v  a2s .  co  m*/
    if (bm != null && !bm.isRecycled()) {
        bm.recycle();
        bm = null;
    }
    return baos.toByteArray();
}