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

The following examples show how to use android.graphics.Bitmap#getAllocationByteCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageCache.java    From graphics-samples with Apache License 2.0 6 votes vote down vote up
/**
 * @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 (Build.VERSION.SDK_INT < 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 2
Source File: ImageCache.java    From RoMote with Apache License 2.0 6 votes vote down vote up
/**
 * @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 3
Source File: ImageCache.java    From malevich with MIT License 6 votes vote down vote up
/**
 * @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 (!Malevich.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 4
Source File: ImageCache.java    From vocefiscal-android with Apache License 2.0 6 votes vote down vote up
/**
 * 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 5
Source File: ImageCache.java    From android-DisplayingBitmaps with Apache License 2.0 6 votes vote down vote up
/**
 * @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 6
Source File: ImageCache.java    From BubbleCloudView with MIT License 6 votes vote down vote up
/**
 * @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
Source File: ImageCache.java    From ImageFrame with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 8
Source File: ImageLoading.java    From actor-platform with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Loading image with reuse bitmap
 *
 * @param source image source
 * @param dest   destination bitmap
 * @return loaded bitmap result
 * @throws ImageLoadException if it is unable to load image
 */
private static ReuseResult loadBitmapReuse(ImageSource source, Bitmap dest) throws ImageLoadException {
    ImageMetadata metadata = source.getImageMetadata();
    boolean tryReuse = false;
    if (dest.isMutable()) {
        if (Build.VERSION.SDK_INT >= 19) {
            tryReuse = dest.getAllocationByteCount() >= metadata.getW() * metadata.getH() * 4;
        } else if (Build.VERSION.SDK_INT >= 11) {
            if (metadata.getFormat() == ImageFormat.JPEG || metadata.getFormat() == ImageFormat.PNG) {
                tryReuse = dest.getWidth() == metadata.getW()
                        && dest.getHeight() == metadata.getH();
            }
        }
    }

    if (tryReuse) {
        return source.loadBitmap(dest);
    } else {
        return new ReuseResult(loadBitmap(source), false);
    }
}
 
Example 9
Source File: BitmapLruPool.java    From jus with Apache License 2.0 6 votes vote down vote up
/**
 * 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 bitmap
 * @return size in bytes
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
public static int getBitmapSize(Bitmap bitmap) {
    if(bitmap==null) return 0;
    // 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 10
Source File: Thumbnail.java    From UMS-Interface with GNU General Public License v3.0 5 votes vote down vote up
public static int getBitmapSize(Bitmap bitmap)
{
    int size = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        size = bitmap.getAllocationByteCount();
    }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1)
    {
        size = bitmap.getByteCount();
    }else
    {
        size = bitmap.getWidth()*bitmap.getHeight()*3;
    }
    return size;
}
 
Example 11
Source File: ASFBitmapRequest.java    From EasyVolley with Apache License 2.0 5 votes vote down vote up
private int getBitmapSize(Bitmap response) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return response.getAllocationByteCount();
    }
    else {
        return response.getByteCount();
    }
}
 
Example 12
Source File: BitmapCompat.java    From attendee-checkin with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the byte size of the bitmap.
 *
 * @param bitmap The bitmap
 * @return The size in bytes
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
public static int getAllocationByteCount(Bitmap bitmap) {
    if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT) {
        return bitmap.getAllocationByteCount();
    } else if (Build.VERSION_CODES.HONEYCOMB_MR1 <= Build.VERSION.SDK_INT) {
        return bitmap.getByteCount();
    } else {
        return bitmap.getRowBytes() * bitmap.getHeight();
    }
}
 
Example 13
Source File: CameraActivity.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
/**
 * 根据图片Uri 获取Bitmap
 *
 * @param destUrl
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
private void ProcessResult(Uri destUrl) {
    String pathName = FileHelper.stripFileProtocol(destUrl.toString());
    showBitmapInfos(pathName);
    Bitmap bitmap = BitmapFactory.decodeFile(pathName);
    if (bitmap != null) {

        //添加图片到相册
        if (insertToGallery) {
            insertToGallery(destUrl);
        }


        if (needCompress) {
            bitmap = getCompressedBitmap(bitmap);

        }

        if (needSample) {
            bitmap = getRealCompressedBitmap(pathName, screenWidth, screenHeight);
        }

        mImageView.setImageBitmap(bitmap);
        float count = bitmap.getByteCount() / M_RATE;
        float all = bitmap.getAllocationByteCount() / M_RATE;
        String result = "这张图片占用内存大小:\n" +
                "bitmap.getByteCount()== " + count + "M\n" +
                "bitmap.getAllocationByteCount()= " + all + "M";
        info.setText(result);
        Log.e(TAG, result);
    } else {
        TT.showLToast(mContext, "fail");
    }
}
 
Example 14
Source File: ImageUtils.java    From RxjavaSample with MIT License 5 votes vote down vote up
/**
 * 获取Bitmap大小
 * @param bitmap
 * @return
 */
@SuppressLint("NewApi")
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 15
Source File: BitmapLruCache.java    From soas with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
@Override
protected int sizeOf(String key, Bitmap bitmap) {
	// The cache size will be measured in kilobytes rather than
       // number of items.
   	if (Build.VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
   		return bitmap.getAllocationByteCount() / 1024;
   	} else if (Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1) {
           return bitmap.getByteCount() / 1024;
   	} else {
           return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
       }
}
 
Example 16
Source File: AppUtils.java    From Reader with Apache License 2.0 5 votes vote down vote up
/**
 * 得到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();
}
 
Example 17
Source File: FrameSeqDecoder.java    From APNG4Android with Apache License 2.0 5 votes vote down vote up
protected Bitmap obtainBitmap(int width, int height) {
    Bitmap ret = null;
    Iterator<Bitmap> iterator = cacheBitmaps.iterator();
    while (iterator.hasNext()) {
        int reuseSize = width * height * 4;
        ret = iterator.next();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (ret != null && ret.getAllocationByteCount() >= reuseSize) {
                iterator.remove();
                if (ret.getWidth() != width || ret.getHeight() != height) {
                    ret.reconfigure(width, height, Bitmap.Config.ARGB_8888);
                }
                ret.eraseColor(0);
                return ret;
            }
        } else {
            if (ret != null && ret.getByteCount() >= reuseSize) {
                if (ret.getWidth() == width && ret.getHeight() == height) {
                    iterator.remove();
                    ret.eraseColor(0);
                }
                return ret;
            }
        }
    }

    try {
        Bitmap.Config config = Bitmap.Config.ARGB_8888;
        ret = Bitmap.createBitmap(width, height, config);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
    }
    return ret;
}
 
Example 18
Source File: BitmapUtil.java    From AndroidBase with Apache License 2.0 5 votes vote down vote up
/**
 * 得到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 19
Source File: Util.java    From giffun with Apache License 2.0 5 votes vote down vote up
/**
 * 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 20
Source File: BucketsBitmapPool.java    From fresco with MIT License 2 votes vote down vote up
/**
 * 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();
}