Example usage for android.graphics Bitmap isRecycled

List of usage examples for android.graphics Bitmap isRecycled

Introduction

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

Prototype

public final boolean isRecycled() 

Source Link

Document

Returns true if this bitmap has been recycled.

Usage

From source file:Main.java

public static Bitmap big(Bitmap bitmap, float scale) {
    if (bitmap == null) {
        return null;
    }//from  w  w w .  j ava  2 s. c o  m
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);
    Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    if (!bitmap.isRecycled()) {
        bitmap.recycle();
    }
    return resizeBmp;
}

From source file:Main.java

/**
 * Returns the in memory size of the given {@link Bitmap} in bytes.
 *///ww  w .ja v  a 2  s.  co  m
@TargetApi(Build.VERSION_CODES.KITKAT)
public static int getBitmapByteSize(Bitmap bitmap) {
    // The return value of getAllocationByteCount silently changes for recycled bitmaps from the
    // internal buffer size to row bytes * height. To avoid random inconsistencies in caches, we
    // instead assert here.
    if (bitmap.isRecycled()) {
        throw new IllegalStateException("Cannot obtain size for recycled Bitmap: " + bitmap + "["
                + bitmap.getWidth() + "x" + bitmap.getHeight() + "] " + bitmap.getConfig());
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // Workaround for KitKat initial release NPE in Bitmap, fixed in MR1. See issue #148.
        try {
            return bitmap.getAllocationByteCount();
        } catch (NullPointerException e) {
            // Do nothing.
        }
    }
    return bitmap.getHeight() * bitmap.getRowBytes();
}

From source file:Main.java

public static Bitmap rotateImageView(int angle, Bitmap bitmap) {

    if (bitmap == null)
        return null;

    Matrix matrix = new Matrix();
    matrix.postRotate(angle);//w  w  w .  ja  v a  2  s  .  c  o  m

    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
            true);
    if (bitmap != null && !bitmap.isRecycled()) {
        bitmap = null;
    }
    // bitmap.recycle();
    return resizedBitmap;
}

From source file:Main.java

public static Bitmap rotateBitmap(Bitmap source, int angle) {
    Matrix matrix = new Matrix();
    if (angle > 0)
        matrix.setRotate(angle);/*w ww.j  a  v a 2s. co  m*/
    Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    if (bitmap != source && !source.isRecycled())
        source.recycle();
    return bitmap;
}

From source file:Main.java

public static Bitmap cutBitmap(int newWidth, int newHeight, Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }//from w  ww.  j a v a  2 s.  c  o  m
    if (bitmap.getWidth() > newWidth) {
        int startX = (bitmap.getWidth() - newWidth) / 2;
        Bitmap targetBitmap = Bitmap.createBitmap(bitmap, startX, 0, newWidth, newHeight);
        if (!bitmap.isRecycled()) {
            bitmap.recycle();
        }
        return targetBitmap;
    } else if (bitmap.getHeight() > newHeight) {
        int startY = (bitmap.getHeight() - newHeight) / 2;
        Bitmap targetBitmap = Bitmap.createBitmap(bitmap, 0, startY, newWidth, newHeight);
        if (!bitmap.isRecycled()) {
            bitmap.recycle();
        }
        return targetBitmap;
    }
    return bitmap;
}

From source file:Main.java

private static List<int[]> getPixels(Bitmap image) {
    int width = image.getWidth();
    int height = image.getHeight();
    List<int[]> res = new ArrayList<>();
    List<Integer> t = new ArrayList<>();
    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            if (!image.isRecycled())
                t.add(image.getPixel(col, row));
        }//  w  w  w .  j a va 2  s .  com
    }
    for (int i = 0; i < t.size(); i += 10) {
        int[] rr = new int[3];
        int argb = t.get(i);
        rr[0] = (argb >> 16) & 0xFF;
        rr[1] = (argb >> 8) & 0xFF;
        rr[2] = (argb) & 0xFF;
        if (!(rr[0] > 250 && rr[1] > 250 && rr[2] > 250)) {
            res.add(rr);
        }
    }
    return res;
}

From source file:Main.java

public static File bitmapToFile(Context context, Bitmap bitmap) {
    File outputFile = getTempFile(context);
    FileOutputStream fos = null;//from  www .ja  v a  2 s . c o m
    try {
        fos = new FileOutputStream(outputFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (!bitmap.isRecycled()) {
        bitmap.recycle();
    }

    return outputFile;
}

From source file:Main.java

public static boolean putBitmapToFile(Bitmap bitmap, File file) {
    if (bitmap == null || file == null)
        return false;
    FileOutputStream fos = null;//from  w ww .  ja va 2s.c o m
    try {
        fos = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (fos != null)
            try {
                fos.flush();
                fos.close();
                if (!bitmap.isRecycled())
                    bitmap.recycle();
            } catch (IOException ignored) {
            }
    }
    return false;
}

From source file:Main.java

public static int saveToSdCard(String fileName, Bitmap bitmap) {
    int ret = 0;//www  .j  ava  2  s .  c  o m
    PrintStream out = null;
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        return -1;
    }
    File file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + fileName);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdir();
    }
    try {
        out = new PrintStream(new FileOutputStream(file));
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        ret = -2;
    } finally {
        out.flush();
        out.close();
        if (!bitmap.isRecycled())
            bitmap.recycle();
    }

    return ret;
}

From source file:Main.java

public static Bitmap createCroppedScaleBitmap(Bitmap src, int reqWidth, int reqHeight) {
    final int bWidth = src.getWidth();
    final int bHeight = src.getHeight();
    Matrix matrix = new Matrix();
    int maxSize = Math.max(reqHeight, reqWidth);
    float scaleX;
    if (bWidth * bHeight < reqWidth * reqHeight)
        scaleX = 0;/*  ww w  .ja  va  2 s  .  c  o  m*/
    else {
        if (bWidth > bHeight) {
            scaleX = (float) maxSize / bWidth;
        } else
            scaleX = (float) maxSize / bHeight;
    }
    Bitmap sourceBitmap;
    if (scaleX > 0 && scaleX != 1) {
        matrix.setScale(scaleX, scaleX);
        sourceBitmap = Bitmap.createBitmap(src, 0, 0, bWidth, bHeight, matrix, true);
        if (sourceBitmap != src && !src.isRecycled())
            src.recycle();
    } else
        sourceBitmap = src;
    return sourceBitmap;
}