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

The following examples show how to use android.graphics.Bitmap#getByteCount() . 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: RoMote   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 2
Source Project: Simpler   File: BitmapUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 图片按比例大小压缩方法
 *
 * @param bitmap
 * @param sizeM  图片大小(以MB为单位)
 * @return
 */
public static Bitmap getImage(Bitmap bitmap, int sizeM) {
    Bitmap bmp = bitmap;
    // 创建操作图片用的matrix对象
    Matrix matrix = new Matrix();
    // 获取这个图片的宽和高
    do {
        float width = bmp.getWidth();
        float height = bmp.getHeight();
        // 缩放图片动作
        matrix.postScale(0.8F, 0.8F);
        bmp = Bitmap.createBitmap(bmp, 0, 0, (int) width,
                (int) height, matrix, true);
    } while (bmp.getByteCount() > sizeM * 1024 * 1024);
    return bmp;
}
 
Example 3
Source Project: tns-core-modules-widgets   File: Cache.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(VERSION_CODES.KITKAT)
public static int getBitmapSize(Bitmap bitmap) {
    // 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 4
Source Project: malevich   File: ImageCache.java    License: MIT License 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 (Malevich.Utils.hasKitKat()) {
        return bitmap.getAllocationByteCount();
    }

    if (Malevich.Utils.hasHoneycombMR1()) {
        return bitmap.getByteCount();
    }

    // Pre HC-MR1
    return bitmap.getRowBytes() * bitmap.getHeight();
}
 
Example 5
Source Project: cube-sdk   File: ImageProvider.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(19) // @TargetApi(VERSION_CODES.KITKAT)
public static long getBitmapSize(BitmapDrawable value) {
    if (null == value) {
        return 0;
    }
    Bitmap bitmap = value.getBitmap();

    // From KitKat onward use getAllocationByteCount() as allocated bytes can potentially be
    // larger than bitmap byte count.
    if (Version.hasKitKat()) {
        return bitmap.getAllocationByteCount();
    }

    if (Version.hasHoneycombMR1()) {
        return bitmap.getByteCount();
    }

    // Pre HC-MR1
    return bitmap.getRowBytes() * bitmap.getHeight();
}
 
Example 6
Source Project: ListViewVariants   File: ImageCache.java    License: Apache License 2.0 5 votes vote down vote up
public static int getBitmapSize(Bitmap bitmap)
{
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB_MR1)
  return bitmap.getByteCount();
// Pre HC-MR1
return bitmap.getRowBytes()*bitmap.getHeight();
}
 
Example 7
Source Project: Dali   File: LegacySDKUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * returns the bytesize of the give bitmap
 */
public 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();
    }
}
 
Example 8
Source Project: sketch   File: SketchUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取 {@link Bitmap} 占用内存大小,单位字节
 */
public static int getByteCount(@Nullable Bitmap bitmap) {
    // bitmap.isRecycled()过滤很关键,在4.4以及以下版本当bitmap已回收时调用其getAllocationByteCount()方法将直接崩溃
    if (bitmap == null || bitmap.isRecycled()) {
        return 0;
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return bitmap.getAllocationByteCount();
    } else {
        return bitmap.getByteCount();
    }
}
 
Example 9
Source Project: bither-bitmap-sample   File: ImageCache.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get the size in bytes of a bitmap.
 * 
 * @param bitmap
 * @return size in bytes
 */
@TargetApi(12)
public static int getBitmapSize(Bitmap bitmap) {
	if (Utils.hasHoneycombMR1()) {
		return bitmap.getByteCount();
	}
	// Pre HC-MR1
	return bitmap.getRowBytes() * bitmap.getHeight();
}
 
Example 10
Source Project: wasp   File: ImageCreator.java    License: Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private int getBitmapSize(Bitmap bitmap) {
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
    return bitmap.getRowBytes() * bitmap.getHeight();
  }
  return bitmap.getByteCount();
}
 
Example 11
Source Project: AndroidBasicProject   File: BitmapUtil.java    License: MIT License 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 12
Source Project: bubble   File: Utils.java    License: MIT License 5 votes vote down vote up
public static int calculateBitmapSize(Bitmap bitmap) {
    int sizeInBytes;
    if (Utils.isHoneycombMR1orLater()) {
        sizeInBytes = bitmap.getByteCount();
    }
    else {
        sizeInBytes = bitmap.getRowBytes() * bitmap.getHeight();
    }
    return sizeInBytes / 1024;
}
 
Example 13
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 14
Source Project: PLDroidShortVideo   File: BitmapUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static byte[] loadPhotoRGBABytes(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }
    byte[] bytes = new byte[bitmap.getByteCount()];
    ByteBuffer rgbaBuffer = ByteBuffer.wrap(bytes);
    bitmap.copyPixelsToBuffer(rgbaBuffer);
    return bytes;
}
 
Example 15
Source Project: Ticket-Analysis   File: Utils.java    License: MIT License 5 votes vote down vote up
/**
 * Get the size in bytes of a bitmap.
 *
 * @param bitmap
 * @return size in bytes
 */
@TargetApi(12)
public static int getBitmapSize(Bitmap bitmap) {
    if (Utils.hasHoneycombMR1()) {
        return bitmap.getByteCount();
    }
    // Pre HC-MR1
    return bitmap.getRowBytes() * bitmap.getHeight();
}
 
Example 16
Source Project: ByakuGallery   File: TileBitmapDrawable.java    License: Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
private static int getBitmapSize(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();
    }
    return bitmap.getRowBytes() * bitmap.getHeight();
}
 
Example 17
Source Project: Android-ImageManager   File: Utils.java    License: MIT License 4 votes vote down vote up
public static int getSizeInBytes(final Bitmap bitmap) {
    return bitmap.getByteCount();
}
 
Example 18
Source Project: MultiView   File: BitmapLruCache.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected int sizeOf(Uri key, Bitmap value) {
    return value.getByteCount() / 1024;
}
 
Example 19
private static void findImageViewInViewTree(View curNode,List<ImageViewInfo> imageList) {

        if (curNode.getVisibility() != View.VISIBLE) {
            return ;
        }
        if (curNode instanceof ViewGroup) {
            ViewGroup curNodeGroup = (ViewGroup) curNode;
            for (int i = 0; i < curNodeGroup.getChildCount(); i++) {
                findImageViewInViewTree(curNodeGroup.getChildAt(i),imageList);
            }
        } else {
            if (curNode instanceof ImageView) {
                ImageViewInfo imageViewInfo = new ImageViewInfo();
                ImageView curImage = (ImageView) curNode;
                Drawable drawable = curImage.getDrawable();
                if (drawable instanceof BitmapDrawable) {
                    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                    imageViewInfo.imageHeight = bitmap.getHeight();
                    imageViewInfo.imageWidth = bitmap.getWidth();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {     //API 19
                        imageViewInfo.imageSize = bitmap.getAllocationByteCount();
                    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12
                        imageViewInfo.imageSize = bitmap.getByteCount();
                    } else {
                        imageViewInfo.imageSize = bitmap.getRowBytes() * bitmap.getHeight();
                    }

                    if(bitmap.getHeight() * bitmap.getWidth() > curImage.getMeasuredHeight() * curImage.getMeasuredWidth()){
                        imageViewInfo.imgViewInfo = MyUtil.printImageView(curImage);
                        imageList.add(imageViewInfo);
                    }

                }else {
                    imageViewInfo.imageHeight = drawable.getIntrinsicHeight();
                    imageViewInfo.imageWidth = drawable.getIntrinsicWidth();
                    if(imageViewInfo.imageHeight * imageViewInfo.imageWidth > curImage.getMeasuredHeight() * curImage.getMeasuredWidth()){
                        imageViewInfo.imgViewInfo = MyUtil.printImageView(curImage);
                        imageList.add(imageViewInfo);
                    }
                }

            }
        }
        return ;
    }
 
Example 20
Source Project: J2ME-Loader   File: Image.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected int sizeOf(String key, Bitmap value) {
	return value.getByteCount();
}