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

The following examples show how to use android.graphics.Bitmap#getAllocationByteCount() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: graphics-samples   File: ImageCache.java    License: 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 Project: jus   File: BitmapLruPool.java    License: 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 3
/**
 * 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 4
Source Project: ImageFrame   File: ImageCache.java    License: 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 5
Source Project: BubbleCloudView   File: ImageCache.java    License: 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 6
/**
 * @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 Project: vocefiscal-android   File: ImageCache.java    License: 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 8
Source Project: malevich   File: ImageCache.java    License: 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 9
Source Project: RoMote   File: ImageCache.java    License: 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 10
Source Project: giffun   File: Util.java    License: 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 11
Source Project: APNG4Android   File: FrameSeqDecoder.java    License: 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 12
Source Project: Reader   File: AppUtils.java    License: 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 13
Source Project: EasyVolley   File: ASFBitmapRequest.java    License: 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 14
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 15
Source Project: attendee-checkin   File: BitmapCompat.java    License: 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 16
/**
 * 根据图片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 17
Source Project: RxjavaSample   File: ImageUtils.java    License: 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 18
Source Project: soas   File: BitmapLruCache.java    License: 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 19
Source Project: AndroidBase   File: BitmapUtil.java    License: 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 20
Source Project: fresco   File: BucketsBitmapPool.java    License: 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();
}