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 blurBitmap(Context context, Bitmap bitmap, float radius) {
    Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    RenderScript rs = RenderScript.create(context);

    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
    Allocation allOut = Allocation.createFromBitmap(rs, bitmap);

    blurScript.setRadius(radius);//  w w  w . j av a 2s.co m

    blurScript.setInput(allIn);
    blurScript.forEach(allOut);

    allOut.copyTo(outBitmap);

    bitmap.recycle();
    rs.destroy();

    return outBitmap;
}

From source file:Main.java

public static Bitmap setupFrame(Bitmap bitmap, int width, int color) {
    if (bitmap.getWidth() <= width * 2 || bitmap.getHeight() <= width * 2) {
        return bitmap;
    }//from www. ja va  2 s .  c  o  m

    Bitmap bp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

    Canvas canvas = new Canvas(bp);
    canvas.drawBitmap(bitmap, 0, 0, new Paint());
    Paint paint = new Paint();
    paint.setColor(color);
    paint.setStrokeWidth(width);
    paint.setStyle(Style.STROKE);
    canvas.drawRect(0, 0, canvas.getWidth() - width, canvas.getHeight() - width, paint);

    bitmap.recycle();
    return bp;
}

From source file:Main.java

public static byte[] bmpToByteArray2(final Bitmap bmp, final boolean needRecycle) {

    int i;/*from www  .  ja v  a 2s .  c  om*/
    int j;
    if (bmp.getHeight() > bmp.getWidth()) {
        i = bmp.getWidth();
        j = bmp.getWidth();
    } else {
        i = bmp.getHeight();
        j = bmp.getHeight();
    }

    Bitmap localBitmap = Bitmap.createBitmap(i, j, Bitmap.Config.RGB_565);
    Canvas localCanvas = new Canvas(localBitmap);

    while (true) {
        localCanvas.drawBitmap(bmp, new Rect(0, 0, i, j), new Rect(0, 0, i, j), null);
        if (needRecycle)
            bmp.recycle();
        ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
        localBitmap.compress(Bitmap.CompressFormat.JPEG, 100, localByteArrayOutputStream);
        localBitmap.recycle();
        byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
        try {
            localByteArrayOutputStream.close();
            return arrayOfByte;
        } catch (Exception e) {
            //F.out(e);
        }
        i = bmp.getHeight();
        j = bmp.getHeight();
    }
}

From source file:Main.java

public static Bitmap getPlusBitmap(Context context, String s) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);//w  w w.j  av  a 2s.  c  o m
    Bitmap bitmap = BitmapFactory.decodeStream(getCommunityPicInputStream(s));
    Bitmap bitmap1;
    if (bitmap != null) {
        Bitmap bitmap2 = BitmapFactory.decodeResource(context.getResources(), 0x7f02003f);
        bitmap1 = Bitmap.createBitmap(bitmap.getWidth() + bitmap2.getWidth() / 2,
                bitmap.getHeight() + bitmap2.getHeight() / 2, android.graphics.Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap1);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawBitmap(bitmap, 0.0F, 0.0F, paint);
        bitmap.recycle();
        canvas.drawBitmap(bitmap2, bitmap.getWidth() - bitmap2.getWidth() / 2,
                bitmap.getHeight() - bitmap2.getHeight() / 2, paint);
        bitmap2.recycle();
    } else {
        bitmap1 = null;
    }
    return bitmap1;
}

From source file:Main.java

public static Bitmap resizeBitmapByScale(Bitmap bitmap, float scale, boolean recycle) {
    int width = Math.round(bitmap.getWidth() * scale);
    int height = Math.round(bitmap.getHeight() * scale);
    if (width == bitmap.getWidth() && height == bitmap.getHeight())
        return bitmap;
    Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap));
    Canvas canvas = new Canvas(target);
    canvas.scale(scale, scale);/*  w w  w .  j av a  2s .c  o m*/
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle)
        bitmap.recycle();
    return target;
}

From source file:Main.java

public static String saveImg(Bitmap b, String name) throws Exception {
    String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "QuCai/shareImg/";
    File mediaFile = new File(path + File.separator + name + ".jpg");
    if (mediaFile.exists()) {
        mediaFile.delete();/*from www .j a v  a 2  s.  c  o  m*/

    }
    if (!new File(path).exists()) {
        new File(path).mkdirs();
    }
    mediaFile.createNewFile();
    FileOutputStream fos = new FileOutputStream(mediaFile);
    b.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();
    b.recycle();
    b = null;
    System.gc();
    return mediaFile.getPath();
}

From source file:Main.java

public static Bitmap ResizeBitmap(Bitmap bitmap, int newWidth) {

    int width = bitmap.getWidth();

    int height = bitmap.getHeight();

    float temp = ((float) height) / ((float) width);

    int newHeight = (int) ((newWidth) * temp);

    float scaleWidth = ((float) newWidth) / width;

    float scaleHeight = ((float) newHeight) / height;

    Matrix matrix = new Matrix();

    // resize the bit map

    matrix.postScale(scaleWidth, scaleHeight);

    //         matrix.postRotate(90);

    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

    bitmap.recycle();

    return resizedBitmap;

}

From source file:Main.java

public static String scaleImageFile(String filepath, int targetSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    File file = new File(filepath);
    float scaleFactor = file.length() / targetSize;
    options.inSampleSize = Math.round(scaleFactor);
    Bitmap bitmap = BitmapFactory.decodeFile(filepath, options);

    int dotPos = filepath.lastIndexOf('.');
    String newFilePath = String.format("%s_scaled.%s", filepath.substring(0, dotPos),
            filepath.substring(dotPos + 1, filepath.length()));
    FileOutputStream fos = null;//from ww  w  .ja  v a 2s  . c om
    try {
        fos = new FileOutputStream(newFilePath);
        if (!bitmap.compress(CompressFormat.JPEG, 90, fos)) {
            newFilePath = null;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        newFilePath = null;
    }
    bitmap.recycle();
    return newFilePath;
}

From source file:Main.java

public static Bitmap getRemoveBitmap(Context context, Bitmap bitmap) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*  w  w  w .j a v  a2s  .c  om*/
    Bitmap bitmap1;
    try {
        Bitmap bitmap2 = BitmapFactory.decodeStream(context.getAssets().open("remove@2x.png"));
        bitmap1 = Bitmap.createBitmap(bitmap.getWidth() + bitmap2.getWidth() / 2,
                bitmap.getHeight() + bitmap2.getHeight() / 2, android.graphics.Bitmap.Config.ARGB_8888);
        Canvas canvas1 = new Canvas(bitmap1);
        canvas1.drawARGB(0, 0, 0, 0);
        canvas1.drawBitmap(bitmap, bitmap2.getWidth() / 2, bitmap2.getHeight() / 2, paint);
        bitmap.recycle();
        canvas1.drawBitmap(bitmap2, 0.0F, 0.0F, paint);
        bitmap2.recycle();
    } catch (IOException ioexception) {
        ioexception.printStackTrace();
        bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                android.graphics.Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap1);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawBitmap(bitmap, 0.0F, 0.0F, paint);
        bitmap.recycle();
    }
    return bitmap1;
}

From source file:Main.java

public static Bitmap getArtworkQuick(Context context, long album_id, int w, int h) {
    // NOTE: There is in fact a 1 pixel border on the right side in the
    // ImageView/*  ww  w. j a v  a 2  s. c  o  m*/
    // used to display this drawable. Take it into account now, so we don't
    // have to
    // scale later.
    w -= 1;
    ContentResolver res = context.getContentResolver();
    Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
    if (uri != null) {
        ParcelFileDescriptor fd = null;
        try {
            fd = res.openFileDescriptor(uri, "r");
            int sampleSize = 1;

            // Compute the closest power-of-two scale factor
            // and pass that to sBitmapOptionsCache.inSampleSize, which will
            // result in faster decoding and better quality
            sBitmapOptionsCache.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, sBitmapOptionsCache);
            int nextWidth = sBitmapOptionsCache.outWidth >> 1;
            int nextHeight = sBitmapOptionsCache.outHeight >> 1;
            while (nextWidth > w && nextHeight > h) {
                sampleSize <<= 1;
                nextWidth >>= 1;
                nextHeight >>= 1;
            }

            sBitmapOptionsCache.inSampleSize = sampleSize;
            sBitmapOptionsCache.inJustDecodeBounds = false;
            Bitmap b = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, sBitmapOptionsCache);

            if (b != null) {
                // finally rescale to exactly the size we need
                if (sBitmapOptionsCache.outWidth != w || sBitmapOptionsCache.outHeight != h) {
                    Bitmap tmp = Bitmap.createScaledBitmap(b, w, h, true);
                    // Bitmap.createScaledBitmap() can return the same
                    // bitmap
                    if (tmp != b)
                        b.recycle();
                    b = tmp;
                }
            }

            return b;
        } catch (FileNotFoundException e) {
        } finally {
            try {
                if (fd != null)
                    fd.close();
            } catch (IOException e) {
            }
        }
    }
    return null;
}