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 File bitmapToFile(Context context, Bitmap bitmap) {
    File outputFile = getTempFile(context);
    FileOutputStream fos = null;//from w  ww  .j a  v  a  2s  .co  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 Bitmap greyScale(Bitmap source) {
    int width = source.getWidth();
    int height = source.getHeight();

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    ColorMatrix saturation = new ColorMatrix();
    saturation.setSaturation(0f);//  w w  w. j a v  a 2 s  .  co  m
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(saturation));
    canvas.drawBitmap(source, 0, 0, paint);
    source.recycle();

    if (source != bitmap) {
        source.recycle();
    }

    return bitmap;
}

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

/**
 * https://gist.github.com/PrashamTrivedi/809d2541776c8c141d9a
 *//*from ww  w  .jav  a 2s  .  c  o m*/
public static Bitmap shotRecyclerView(RecyclerView view) {
    RecyclerView.Adapter adapter = view.getAdapter();
    Bitmap bigBitmap = null;
    if (adapter != null) {
        int size = adapter.getItemCount();
        int height = 0;
        Paint paint = new Paint();
        int iHeight = 0;
        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

        // Use 1/8th of the available memory for this memory cache.
        final int cacheSize = maxMemory / 8;
        LruCache<String, Bitmap> bitmaCache = new LruCache<>(cacheSize);
        for (int i = 0; i < size; i++) {
            RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i));
            adapter.onBindViewHolder(holder, i);
            holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(),
                    holder.itemView.getMeasuredHeight());
            holder.itemView.setDrawingCacheEnabled(true);
            holder.itemView.buildDrawingCache();
            Bitmap drawingCache = holder.itemView.getDrawingCache();
            if (drawingCache != null) {

                bitmaCache.put(String.valueOf(i), drawingCache);
            }
            height += holder.itemView.getMeasuredHeight();
        }

        bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888);
        Canvas bigCanvas = new Canvas(bigBitmap);
        Drawable lBackground = view.getBackground();
        if (lBackground instanceof ColorDrawable) {
            ColorDrawable lColorDrawable = (ColorDrawable) lBackground;
            int lColor = lColorDrawable.getColor();
            bigCanvas.drawColor(lColor);
        }

        for (int i = 0; i < size; i++) {
            Bitmap bitmap = bitmaCache.get(String.valueOf(i));
            bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint);
            iHeight += bitmap.getHeight();
            bitmap.recycle();
        }
    }
    return bigBitmap;
}

From source file:ca.psiphon.ploggy.Robohash.java

public static Bitmap getRobohash(Context context, boolean cacheCandidate, byte[] data)
        throws Utils.ApplicationError {
    try {/*from w w w. j  av  a2 s.  c o  m*/
        MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
        byte[] digest = sha1.digest(data);

        String key = Utils.formatFingerprint(digest);
        Bitmap cachedBitmap = mCache.get(key);
        if (cachedBitmap != null) {
            return cachedBitmap;
        }

        ByteBuffer byteBuffer = ByteBuffer.wrap(digest);
        byteBuffer.order(ByteOrder.BIG_ENDIAN);
        // TODO: SecureRandom SHA1PRNG (but not LinuxSecureRandom)
        Random random = new Random(byteBuffer.getLong());

        AssetManager assetManager = context.getAssets();

        if (mConfig == null) {
            mConfig = new JSONObject(loadAssetToString(assetManager, CONFIG_FILENAME));
        }

        int width = mConfig.getInt("width");
        int height = mConfig.getInt("height");

        JSONArray colors = mConfig.getJSONArray("colors");
        JSONArray parts = colors.getJSONArray(random.nextInt(colors.length()));

        Bitmap robotBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas robotCanvas = new Canvas(robotBitmap);

        for (int i = 0; i < parts.length(); i++) {
            JSONArray partChoices = parts.getJSONArray(i);
            String selection = partChoices.getString(random.nextInt(partChoices.length()));
            Bitmap partBitmap = loadAssetToBitmap(assetManager, selection);
            Rect rect = new Rect(0, 0, width, height);
            Paint paint = new Paint();
            paint.setAlpha(255);
            robotCanvas.drawBitmap(partBitmap, rect, rect, paint);
            partBitmap.recycle();
        }

        if (cacheCandidate) {
            mCache.set(key, robotBitmap);
        }

        return robotBitmap;

    } catch (IOException e) {
        throw new Utils.ApplicationError(LOG_TAG, e);
    } catch (JSONException e) {
        throw new Utils.ApplicationError(LOG_TAG, e);
    } catch (NoSuchAlgorithmException e) {
        throw new Utils.ApplicationError(LOG_TAG, e);
    }
}

From source file:Main.java

/**
 * get a screen shot with size : width X height.
 *///from ww w  .ja v  a  2  s  .  co m
public static Bitmap getScreenShot(Activity activity, int width, int height) {
    if (activity == null || width < 1 || height < 1) {
        return null;
    }
    Window window = activity.getWindow();
    if (window == null) {
        return null;
    }
    View decorView = window.getDecorView();
    if (decorView == null) {
        return null;
    }
    decorView.setDrawingCacheEnabled(true);
    Bitmap screenShot = decorView.getDrawingCache(true);
    if (screenShot == null) {
        return null;
    }
    Matrix matrix = new Matrix();
    matrix.postScale((float) width / screenShot.getWidth(), (float) height / screenShot.getHeight());
    Bitmap drawingCache = Bitmap.createBitmap(screenShot, 0, 0, screenShot.getWidth(), screenShot.getHeight(),
            matrix, true);
    decorView.destroyDrawingCache();
    screenShot.recycle();
    return drawingCache;
}

From source file:Main.java

public static Bitmap createRoundCornerBitmap(Bitmap bitmap, float roundPx, boolean lt, boolean rt, boolean lb,
        boolean rb) {
    Bitmap roundCornerBitmap = createRoundCornerBitmap(bitmap, roundPx);
    Canvas canvas = new Canvas(roundCornerBitmap);
    canvas.drawARGB(0, 0, 0, 0);//from www .j ava2  s .c o m
    int bw = bitmap.getWidth();
    int bh = bitmap.getHeight();
    int centerW = bw / 2;
    int centerH = bh / 2;

    Paint paint = new Paint();
    Paint defaultPaint = new Paint();
    int color = 0xff424242;
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    paint.setAntiAlias(true);
    paint.setColor(color);

    Bitmap cutBitmap = null;
    Canvas cutCanvas = null;
    if (!lt) {
        cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
        cutCanvas = new Canvas(cutBitmap);
        cutCanvas.drawRect(0, 0, centerW, centerH, defaultPaint);
        cutCanvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
        cutBitmap.recycle();
    }
    if (!rt) {
        cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
        cutCanvas = new Canvas(cutBitmap);
        cutCanvas.drawRect(centerW, 0, bw, centerH, defaultPaint);
        cutCanvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
        cutBitmap.recycle();
    }
    if (!lb) {
        cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
        cutCanvas = new Canvas(cutBitmap);
        cutCanvas.drawRect(0, centerH, centerW, bh, defaultPaint);
        cutCanvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
        cutBitmap.recycle();
    }
    if (!rb) {
        cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
        cutCanvas = new Canvas(cutBitmap);
        cutCanvas.drawRect(centerW, centerH, bw, bh, defaultPaint);
        cutCanvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint);
        cutBitmap.recycle();
    }
    return roundCornerBitmap;
}

From source file:Main.java

private static Bitmap adjustDisplayImage(String path, int width, int hight) {
    BitmapFactory.Options opts = getBitmapOptions(path);
    int srcWidth = opts.outWidth;
    int srcHeight = opts.outHeight;

    Bitmap compressedBitmap = null;//from   w  w w .  j  a v  a 2 s .  co  m
    Bitmap thumbnailBitmap = null;
    try {
        thumbnailBitmap = getThumbnail(path, width, hight);
        if (null == thumbnailBitmap) {
            return null;
        }
        compressedBitmap = ThumbnailUtils.extractThumbnail(thumbnailBitmap, width, hight, 1);
    } catch (Error e) {

        return null;
    } finally {
        if (thumbnailBitmap != null) {
            thumbnailBitmap.recycle();
            thumbnailBitmap = null;
        }
    }

    return compressedBitmap;
}

From source file:com.geekandroid.sdk.pay.utils.Util.java

public static Bitmap extractThumbNail(final String path, final int height, final int width,
        final boolean crop) {
    Assert.assertTrue(path != null && !path.equals("") && height > 0 && width > 0);

    BitmapFactory.Options options = new BitmapFactory.Options();

    try {/*from   ww w  . j a v a2 s .  c  o  m*/
        options.inJustDecodeBounds = true;
        Bitmap tmp = BitmapFactory.decodeFile(path, options);
        if (tmp != null) {
            tmp.recycle();
            tmp = null;
        }

        Log.d(TAG, "extractThumbNail: round=" + width + "x" + height + ", crop=" + crop);
        final double beY = options.outHeight * 1.0 / height;
        final double beX = options.outWidth * 1.0 / width;
        Log.d(TAG, "extractThumbNail: extract beX = " + beX + ", beY = " + beY);
        options.inSampleSize = (int) (crop ? (beY > beX ? beX : beY) : (beY < beX ? beX : beY));
        if (options.inSampleSize <= 1) {
            options.inSampleSize = 1;
        }

        // NOTE: out of memory error
        while (options.outHeight * options.outWidth / options.inSampleSize > MAX_DECODE_PICTURE_SIZE) {
            options.inSampleSize++;
        }

        int newHeight = height;
        int newWidth = width;
        if (crop) {
            if (beY > beX) {
                newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth);
            } else {
                newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight);
            }
        } else {
            if (beY < beX) {
                newHeight = (int) (newWidth * 1.0 * options.outHeight / options.outWidth);
            } else {
                newWidth = (int) (newHeight * 1.0 * options.outWidth / options.outHeight);
            }
        }

        options.inJustDecodeBounds = false;

        Log.i(TAG, "bitmap required size=" + newWidth + "x" + newHeight + ", orig=" + options.outWidth + "x"
                + options.outHeight + ", sample=" + options.inSampleSize);
        Bitmap bm = BitmapFactory.decodeFile(path, options);
        if (bm == null) {
            Log.e(TAG, "bitmap decode failed");
            return null;
        }

        Log.i(TAG, "bitmap decoded size=" + bm.getWidth() + "x" + bm.getHeight());
        final Bitmap scale = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true);
        if (scale != null) {
            bm.recycle();
            bm = scale;
        }

        if (crop) {
            final Bitmap cropped = Bitmap.createBitmap(bm, (bm.getWidth() - width) >> 1,
                    (bm.getHeight() - height) >> 1, width, height);
            if (cropped == null) {
                return bm;
            }

            bm.recycle();
            bm = cropped;
            Log.i(TAG, "bitmap croped size=" + bm.getWidth() + "x" + bm.getHeight());
        }
        return bm;

    } catch (final OutOfMemoryError e) {
        Log.e(TAG, "decode bitmap failed: " + e.getMessage());
        options = null;
    }

    return null;
}

From source file:Main.java

public static String writeBitmap(byte[] data, int cameraDegree, Rect rect, Rect willTransformRect)
        throws IOException {
    File file = new File(Environment.getExternalStorageDirectory() + "/bookclip/");
    file.mkdir();// w  w  w .ja va 2  s. co m
    String bitmapPath = file.getPath() + "/" + System.currentTimeMillis() + ".png";

    // bitmap rotation, scaling, crop
    BitmapFactory.Options option = new BitmapFactory.Options();
    option.inSampleSize = 2;
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, option);
    Matrix bitmapMatrix = new Matrix();
    bitmapMatrix.setRotate(cameraDegree);
    int x = rect.left, y = rect.top, width = rect.right - rect.left, height = rect.bottom - rect.top;
    Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), bitmapMatrix,
            false);
    // bitmap recycle
    bitmap.recycle();

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(rotateBitmap, willTransformRect.right,
            willTransformRect.bottom - willTransformRect.top, false);
    // rotatebitmap recycle
    rotateBitmap.recycle();

    Bitmap cropBitmap = Bitmap.createBitmap(scaledBitmap, x, y, width, height, null, false);
    // scaledBitmap recycle
    scaledBitmap.recycle();

    // file write
    FileOutputStream fos = new FileOutputStream(new File(bitmapPath));
    cropBitmap.compress(CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();

    // recycle
    cropBitmap.recycle();

    return bitmapPath;
}

From source file:Main.java

public static Bitmap rotateBitmap(Bitmap src, int degree) {
    Bitmap ret = null;//from  ww  w.  j  ava2 s  .co  m
    Matrix matrix = new Matrix();
    matrix.postRotate(degree);
    try {
        ret = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
    } catch (OutOfMemoryError ignore) {
    }
    if (ret == null) {
        ret = src;
    }
    if (src != ret) {
        src.recycle();
    }
    return ret;
}