List of usage examples for android.graphics Bitmap recycle
public void recycle()
From source file:Main.java
public static Bitmap getScaledBitmapFromBitmap(Bitmap bmp, int maxWidth, int maxHeight, boolean keepAspectRatio, boolean freeOldBitmap) { if (keepAspectRatio) { float ratio = bmp.getWidth() == 0 ? 1.f : (bmp.getWidth() / (float) bmp.getHeight()); float newRatio = maxHeight == 0 ? 1.f : (maxWidth / (float) maxHeight); if (newRatio > ratio) { maxWidth = (int) ((float) maxHeight * ratio); } else if (newRatio < ratio) { maxHeight = (int) ((float) maxWidth / ratio); }/* w w w . ja va 2 s .com*/ } Bitmap scaled = Bitmap.createScaledBitmap(bmp, maxWidth, maxHeight, true); if (freeOldBitmap && scaled != bmp) { bmp.recycle(); } return scaled; }
From source file:com.cooliris.media.UriTexture.java
public static String writeHttpDataInDirectory(Context context, String uri, String path) { long crc64 = Utils.Crc64Long(uri); if (!isCached(crc64, 1024)) { Bitmap bitmap; try {//from w w w . j a v a 2 s .c om bitmap = UriTexture.createFromUri(context, uri, 1024, 1024, crc64, null); } catch (OutOfMemoryError e) { return null; } catch (IOException e) { return null; } catch (URISyntaxException e) { return null; } bitmap.recycle(); } String fileString = createFilePathFromCrc64(crc64, 1024); try { File file = new File(fileString); if (file.exists()) { // We write a copy of this file String newPath = path + (path.endsWith("/") ? "" : "/") + crc64 + ".jpg"; File newFile = new File(newPath); Utils.Copy(file, newFile); return newPath; } return null; } catch (Exception e) { return null; } }
From source file:Main.java
/** * Cut Bitmap/*from w w w .j av a 2s. c o m*/ * Bitmap memory size is adjusted depending on input dimensions * decodeCropBitmapFromResource */ public static Bitmap decodeCropBitmapFromResource(Resources res, int resId, int x, int y, int width, int height) { Bitmap bitmap; Bitmap croppedBitmap; final BitmapFactory.Options options = new BitmapFactory.Options(); // First decode with inJustDecodeBounds=true to check dimensions options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; // Enables bitmap re-usage options.inMutable = true; /* EFFICIENT Crop ? - Creates new Bitmap */// <- Best option? bitmap = BitmapFactory.decodeResource(res, resId, options); croppedBitmap = Bitmap.createBitmap(bitmap, x, y, width, height); bitmap.recycle(); /* EFFICIENT Crop ? - Reuses bitmap, but it is slow */ // croppedBitmap = BitmapFactory.decodeResource(res, resId, options); // int[] pixels = getPixelsFromBitmap(croppedBitmap, // x, y, // width, height); // croppedBitmap.setPixels(pixels, 0, width, // 0, 0, // width, height); return croppedBitmap; }
From source file:Main.java
/** * @param context//w w w . j a v a2s. c o m * @param data * @return */ @Nullable public static byte[] retrieveSelectedImage(@NonNull Context context, @NonNull Intent data) { InputStream inStream = null; Bitmap bitmap = null; try { inStream = context.getContentResolver().openInputStream(data.getData()); bitmap = BitmapFactory.decodeStream(inStream); final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream); return outStream.toByteArray(); } catch (FileNotFoundException e) { return null; } finally { if (inStream != null) { try { inStream.close(); } catch (IOException ignored) { } } if (bitmap != null) { bitmap.recycle(); } } }
From source file:Main.java
public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xffffffff; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; paint.setAntiAlias(true);/*from ww w.j a v a 2 s.c o m*/ canvas.drawARGB(0, 255, 255, 255); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); bitmap.recycle(); return output; }
From source file:com.ruesga.rview.misc.BitmapUtils.java
@SuppressWarnings("deprecation") public static Bitmap decodeBitmap(File file, int dstWidth, int dstHeight) { // First decode with inJustDecodeBounds=true to check dimensions final Options options = new Options(); options.inScaled = false;/*from w w w . jav a 2 s. c om*/ options.inDither = true; options.inPreferQualityOverSpeed = false; options.inPreferredConfig = Bitmap.Config.ARGB_8888; // Deprecated, but still valid for KitKat and lower apis options.inPurgeable = true; options.inInputShareable = true; options.inJustDecodeBounds = true; BitmapFactory.decodeFile(file.getAbsolutePath(), options); // Decode the bitmap with inSampleSize set options.inSampleSize = calculateBitmapRatio(options, dstWidth, dstHeight); options.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options); if (bitmap == null) { return null; } // Test if the bitmap has exif format, and decode properly Bitmap out = decodeExifBitmap(file, bitmap); if (out != null && !out.equals(bitmap)) { bitmap.recycle(); } return out; }
From source file:Main.java
public static Bitmap getCircleBitmap(Bitmap bitmap, boolean recycleable) { final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(output); final int color = Color.RED; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); paint.setAntiAlias(true);//from w w w .j av a 2s . c o m canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawOval(rectF, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); if (recycleable) { bitmap.recycle(); } return output; }
From source file:Main.java
public static Bitmap decodeCropBitmapFromByteArray(byte[] byteArray, int x, int y, int width, int height) { Bitmap bitmap; Bitmap croppedBitmap;/*from w ww . ja v a2 s . c om*/ final BitmapFactory.Options options = new BitmapFactory.Options(); // First decode with inJustDecodeBounds=true to check dimensions options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, options); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; // Enables bitmap re-usage options.inMutable = true; /* EFFICIENT Crop ? - Creates new Bitmap */// <- Best option? bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, options); croppedBitmap = Bitmap.createBitmap(bitmap, x, y, width, height); bitmap.recycle(); /* EFFICIENT Crop ? - Reuses bitmap, but it is slow */ // croppedBitmap = BitmapFactory.decodeResource(res, resId, options); // int[] pixels = getPixelsFromBitmap(croppedBitmap, // x, y, // width, height); // croppedBitmap.setPixels(pixels, 0, width, // 0, 0, // width, height); return croppedBitmap; }
From source file:Main.java
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = 12; paint.setAntiAlias(true);/*from w w w .j a v a 2 s .co m*/ canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); if (null != bitmap) { bitmap.recycle(); bitmap = null; } return output; }
From source file:Main.java
public static Bitmap roundCorners(final Bitmap source, final float radius) { int width = source.getWidth(); int height = source.getHeight(); Paint paint = new Paint(); paint.setAntiAlias(true);//from w w w . ja va2 s. co m paint.setColor(Color.WHITE); Bitmap clipped = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(clipped); canvas.drawRoundRect(new RectF(0, 0, width, height), radius, radius, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); Bitmap rounded = Bitmap.createBitmap(width, height, Config.ARGB_8888); canvas = new Canvas(rounded); canvas.drawBitmap(source, 0, 0, null); canvas.drawBitmap(clipped, 0, 0, paint); source.recycle(); clipped.recycle(); return rounded; }