Java Code Examples for android.graphics.Bitmap.getAllocationByteCount()

The following are Jave code examples for showing how to use getAllocationByteCount() of the android.graphics.Bitmap class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: DisplayingBitmaps   File: ImageCache.java   View Source Code Vote up 8 votes
/**
 * 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();
}
 
Example 2
Project: GitHub   File: Util.java   View Source Code Vote up 6 votes
/**
 * 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();
}
 
Example 3
Project: FrameAnimationView   File: ImageCache.java   View Source Code Vote up 6 votes
/**
 * @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(Build.VERSION_CODES.KITKAT)
private static boolean canUseForInBitmap(
        Bitmap candidate, BitmapFactory.Options targetOptions) {
    //BEGIN_INCLUDE(can_use_for_inbitmap)
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        // 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)
}
 
Example 4
Project: GitHub   File: Util.java   View Source Code Vote up 6 votes
/**
 * 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();
}
 
Example 5
Project: GitHub   File: BitmapUtil.java   View Source Code Vote up 6 votes
/**
 * @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. Same code as getByteCount() for Honeycomb.
  return bitmap.getRowBytes() * bitmap.getHeight();
}
 
Example 6
Project: Android-Practice   File: ImageCache.java   View Source Code Vote up 6 votes
/**
 * @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)
}
 
Example 7
Project: firefox-tv   File: LocaleListPreference.java   View Source Code Vote up 6 votes
private static byte[] getPixels(final Bitmap b) {
    final int byteCount = b.getAllocationByteCount();

    final ByteBuffer buffer = ByteBuffer.allocate(byteCount);
    try {
        b.copyPixelsToBuffer(buffer);
    } catch (RuntimeException e) {
        // Android throws this if there's not enough space in the buffer.
        // This should never occur, but if it does, we don't
        // really care -- we probably don't need the entire image.
        // This is awful. I apologize.
        if ("Buffer not large enough for pixels".equals(e.getMessage())) {
            return buffer.array();
        }
        throw e;
    }

    return buffer.array();
}
 
Example 8
Project: Habitizer   File: EditProfileActivity.java   View Source Code Vote up 6 votes
/**
 * Gets resized bitmap
 * @param pic
 * @param maxSize
 * @return
 */
public Bitmap getResizedBitmap(Bitmap pic, int maxSize) {

    double div = 90.0;
    Bitmap image = pic.copy(pic.getConfig(), true);
    int size = image.getAllocationByteCount();

    int quality;
    while (size >= maxSize) {
        quality = (int) (div);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        pic.compress(Bitmap.CompressFormat.JPEG, quality, stream);

        byte[] byteArray = stream.toByteArray();
        image = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
        size = byteArray.length;
        div = div * 0.9;
    }
    return image;
}
 
Example 9
Project: GitHub   File: Downsampler.java   View Source Code Vote up 5 votes
@Nullable
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String getBitmapString(Bitmap bitmap) {
  if (bitmap == null) {
    return null;
  }

  String sizeString = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
      ? " (" + bitmap.getAllocationByteCount() + ")" : "";
  return  "[" + bitmap.getWidth() + "x" + bitmap.getHeight() + "] " + bitmap.getConfig()
      + sizeString;
}
 
Example 10
Project: GitHub   File: Utils.java   View Source Code Vote up 5 votes
static int getBitmapBytes(Bitmap bitmap) {
  int result = SDK_INT >= KITKAT ? bitmap.getAllocationByteCount() : bitmap.getByteCount();
  if (result < 0) {
    throw new IllegalStateException("Negative size: " + bitmap);
  }
  return result;
}
 
Example 11
Project: stynico   File: ivip.java   View Source Code Vote up 5 votes
/**
 * Returns the in memory size of the given {@link Bitmap} in bytes.
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
public static int getBitmapByteSize(Bitmap bitmap) {
    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();
}
 
Example 12
Project: yyox   File: PictureUtil.java   View Source Code Vote up 5 votes
/**
 * 得到bitmap的大小
 */
public static int getBitmapSize(Bitmap bitmap) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {    //API 19
        return bitmap.getAllocationByteCount();
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12
        return bitmap.getByteCount();
    }
    // 在低版本中用一行的字节x高度
    return bitmap.getRowBytes() * bitmap.getHeight();                //earlier version
}
 
Example 13
Project: FrameAnimationView   File: ImageCache.java   View Source Code Vote up 5 votes
@TargetApi(Build.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 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return bitmap.getAllocationByteCount();
    }

    return bitmap.getByteCount();
}
 
Example 14
Project: Flora   File: BitmapUtil.java   View Source Code Vote up 5 votes
public static long getSizeInBytes(Bitmap bitmap) {
    if (bitmap == null)
        return 0L;
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
        try {
            return bitmap.getAllocationByteCount();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }
    return bitmap.getByteCount();
}
 
Example 15
Project: cardinalsSample   File: PhotoCaptureUtil.java   View Source Code Vote up 5 votes
public static int getBitmapSize(Bitmap bitmap) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)   //API 19

    {
        return bitmap.getAllocationByteCount();
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1)  //API 12

    {
        return bitmap.getByteCount();
    }
    return bitmap.getRowBytes() * bitmap.getHeight();                //earlier version
}
 
Example 16
Project: Tiny   File: BitmapKit.java   View Source Code Vote up 5 votes
public static long getSizeInBytes(Bitmap bitmap) {
    if (bitmap == null)
        return 0L;
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
        try {
            return bitmap.getAllocationByteCount();
        } catch (Exception e) {
            //ignore...
        }
    }
    return bitmap.getByteCount();
}
 
Example 17
Project: Accessibility   File: BitmapUtils.java   View Source Code Vote up 5 votes
public static int getBitmapSize(Bitmap bitmap){
    if(bitmap != null){
        try{
            if(Build.VERSION.SDK_INT >= AndroidVersionCodes.KITKAT){
                return bitmap.getAllocationByteCount();
            }else if(Build.VERSION.SDK_INT >= AndroidVersionCodes.HONEYCOMB_MR1){
                return bitmap.getByteCount();
            }else{
                return bitmap.getRowBytes() * bitmap.getHeight();
            }
        }catch (Exception e){
        }
    }
    return 0;
}
 
Example 18
Project: tiny   File: BitmapKit.java   View Source Code Vote up 5 votes
public static long getSizeInBytes(Bitmap bitmap) {
    if (bitmap == null)
        return 0L;
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
        try {
            return bitmap.getAllocationByteCount();
        } catch (NullPointerException e) {
            //ignore...
        }
    }
    return bitmap.getByteCount();
}
 
Example 19
Project: boohee_v5.6   File: BitmapCompatKitKat.java   View Source Code Vote up 4 votes
static int getAllocationByteCount(Bitmap bitmap) {
    return bitmap.getAllocationByteCount();
}
 
Example 20
Project: GitHub   File: BitmapPool.java   View Source Code Vote up 2 votes
/**
 * Gets the bucketed size of the value.
 * We don't check the 'validity' of the value (beyond the not-null check). That's handled
 * in {@link #isReusable(Bitmap)}
 * @param value the value
 * @return bucketed size of the value
 */
@Override
protected int getBucketedSizeForValue(Bitmap value) {
  Preconditions.checkNotNull(value);
  return value.getAllocationByteCount();
}