/** Returns the in memory size of the given {@link Bitmap} in bytes. */ @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(); }
/** Returns the in memory size of the given {@link Bitmap}. */ public static int getSize(Bitmap bitmap) { if (Build.VERSION.SDK_INT >= 19) { return bitmap.getAllocationByteCount(); } else { return bitmap.getHeight() * bitmap.getRowBytes(); } }
// TODO get bytes of bitmap private static int byteSizeOf(Bitmap bitmap) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { return bitmap.getAllocationByteCount(); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) { return bitmap.getByteCount(); } else { return bitmap.getRowBytes() * bitmap.getHeight(); } }
protected int a(Object obj, Bitmap bitmap) { int i; if (android.os.Build.VERSION.SDK_INT >= 19) { i = bitmap.getAllocationByteCount(); } else { i = bitmap.getByteCount(); } return (i + 1023) / 1024; }
/** * Get the size in bytes of a bitmap in a BitmapDrawable. Note that from Android 4.4 (KitKat) * onward this returns the allocated memory size of the bitmap which can be larger than the actual * bitmap data byte count (in the case it was re-used). * * @param value * @return size in bytes */ @TargetApi(VERSION_CODES.KITKAT) public static int getBitmapSize(BitmapDrawable value) { Bitmap bitmap = value.getBitmap(); // From KitKat onward use getAllocationByteCount() as allocated bytes can potentially be // larger than bitmap byte count. if (Utils.hasKitKat()) { return bitmap.getAllocationByteCount(); } if (Utils.hasHoneycombMR1()) { return bitmap.getByteCount(); } // Pre HC-MR1 return bitmap.getRowBytes() * bitmap.getHeight(); }
/** * reutilizableParaInBitmap: Checa si un bitmap se puede reutilizar de acuerdo a las opciones * especificadas. * * @param candidato Bitmap que se va a investigar * @param opciones BitmapFactory.Options que el candidato debe satisfacer * @return true si se puede reutilizar este candidato, false de otro modo. */ static boolean reutilizableParaInBitmap(Bitmap candidato, BitmapFactory.Options opciones) { int ancho = opciones.outWidth / opciones.inSampleSize; int alto = opciones.outHeight / opciones.inSampleSize; Bitmap.Config configuracion = candidato.getConfig(); int bytes_por_pixel; // Checa cada configuracion if (configuracion == Bitmap.Config.ARGB_8888) { bytes_por_pixel = 4; } else if (configuracion == Bitmap.Config.RGB_565) { bytes_por_pixel = 2; } else if (configuracion == Bitmap.Config.ARGB_4444) { bytes_por_pixel = 2; } else { bytes_por_pixel = 1; } int conteo_bytes = ancho * alto * bytes_por_pixel; return conteo_bytes <= candidato.getAllocationByteCount(); }
/** * @param candidate - Bitmap to check * @param targetOptions - Options that have the out* value populated * @return true if <code>candidate</code> can be used for inBitmap re-use with <code>targetOptions * </code> */ @TargetApi(VERSION_CODES.KITKAT) private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) { // BEGIN_INCLUDE(can_use_for_inbitmap) if (!Utils.hasKitKat()) { // On earlier versions, the dimensions must match exactly and the inSampleSize must be 1 return candidate.getWidth() == targetOptions.outWidth && candidate.getHeight() == targetOptions.outHeight && targetOptions.inSampleSize == 1; } // From Android 4.4 (KitKat) onward we can re-use if the byte size of the new bitmap // is smaller than the reusable bitmap candidate allocation byte count. int width = targetOptions.outWidth / targetOptions.inSampleSize; int height = targetOptions.outHeight / targetOptions.inSampleSize; int byteCount = width * height * getBytesPerPixel(candidate.getConfig()); return byteCount <= candidate.getAllocationByteCount(); // END_INCLUDE(can_use_for_inbitmap) }
private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // From Android 4.4 (KitKat) onward we can re-use if the byte size // of // the new bitmap is smaller than the reusable bitmap candidate // allocation byte count. int width = targetOptions.outWidth / targetOptions.inSampleSize; int height = targetOptions.outHeight / targetOptions.inSampleSize; int byteCount = width * height * getBytesPerPixel(candidate.getConfig()); return byteCount <= candidate.getAllocationByteCount(); } // On earlier versions, the dimensions must match exactly and the // inSampleSize must be 1 return candidate.getWidth() == targetOptions.outWidth && candidate.getHeight() == targetOptions.outHeight && targetOptions.inSampleSize == 1; }
/** @return size in bytes of the underlying bitmap */ @SuppressLint("NewApi") public static int getSizeInBytes(@Nullable Bitmap bitmap) { if (bitmap == null) { return 0; } // There's a known issue in KitKat where getAllocationByteCount() can throw an NPE. This was // apparently fixed in MR1: http://bit.ly/1IvdRpd. So we do a version check here, and // catch any potential NPEs just to be safe. if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { try { return bitmap.getAllocationByteCount(); } catch (NullPointerException npe) { // Swallow exception and try fallbacks. } } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) { return bitmap.getByteCount(); } // Estimate for earlier platforms. return bitmap.getWidth() * bitmap.getRowBytes(); }
protected int sizeOf(String key, Bitmap value) { if (VERSION.SDK_INT >= 19) { return value.getAllocationByteCount() / 1024; } return (value.getRowBytes() * value.getHeight()) / 1024; }