本文整理汇总了Java中android.graphics.Bitmap.getByteCount方法的典型用法代码示例。如果您正苦于以下问题:Java Bitmap.getByteCount方法的具体用法?Java Bitmap.getByteCount怎么用?Java Bitmap.getByteCount使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.graphics.Bitmap
的用法示例。
在下文中一共展示了Bitmap.getByteCount方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compressImageToMax
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* Compress an image as much as possible until the size in bytes is no more than the specified amount
* @param image the image to compress
* @param maxBytes the maximum size of the image in bytes
* @return the compressed image if it was possible to get it to that size, or null otherwise
*/
public static Bitmap compressImageToMax(Bitmap image, int maxBytes){
int oldSize = image.getByteCount();
// attempt to resize the image as much as possible while valid
while (image != null && image.getByteCount() > maxBytes){
// Prevent image from becoming too small
if (image.getWidth() <= 20 || image.getHeight() <= 20)
return null;
// scale down the image by a factor of 2
image = Bitmap.createScaledBitmap(image, image.getWidth() / 2, image.getHeight() / 2, false);
// the byte count did not change for some reason, can not be made any smaller
if (image.getByteCount() == oldSize)
return null;
oldSize = image.getByteCount();
}
return image;
}
示例2: getSizeInBytes
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* @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();
}
示例3: getBitmapSize
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* 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();
}
示例4: getBitmapSize
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* 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)
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();
}
示例5: getSizeInBytes
import android.graphics.Bitmap; //导入方法依赖的package包/类
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();
}
示例6: getSizeInBytes
import android.graphics.Bitmap; //导入方法依赖的package包/类
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();
}
示例7: onActivityResult
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* Handle the results of the image selection activity
* @param requestCode id of the finished activity
* @param resultCode code representing whether that activity was successful or not
* @param data the data returned from that activity
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
/*
* based on Benjamin's answer from
* https://stackoverflow.com/questions/5309190/android-pick-images-from-gallery
*
* Permalink to specific comment:
* https://stackoverflow.com/a/5309965
*/
if (requestCode == SELECT_IMAGE_CODE && resultCode == Activity.RESULT_OK && data != null) {
try {
InputStream inputStream = getContentResolver().openInputStream(data.getData());
Bitmap chosenImage = BitmapFactory.decodeStream(inputStream);
// attempt to resize the image if necessary
chosenImage = ImageUtilities.compressImageToMax(chosenImage, MAX_IMAGE_SIZE);
if (chosenImage == null) {
image = null;
updateImage();
} else if (chosenImage.getByteCount() <= MAX_IMAGE_SIZE) {
image = chosenImage;
updateImage();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例8: testLargeImage
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* Testing an image that is too large to fit in the system, and must be resized
*/
public void testLargeImage() throws IOException {
Bitmap bitmap = BitmapFactory.decodeStream(getActivity().getAssets().open("testimages/large.jpg"));
int size = bitmap.getByteCount();
Bitmap newBitmap = ImageUtilities.compressImageToMax(bitmap, CreateHabitEventActivity.MAX_IMAGE_SIZE);
assertTrue(newBitmap != null);
assertTrue(size > CreateHabitEventActivity.MAX_IMAGE_SIZE);
assertTrue(newBitmap.getByteCount() <= CreateHabitEventActivity.MAX_IMAGE_SIZE);
}
示例9: getBitmapSize
import android.graphics.Bitmap; //导入方法依赖的package包/类
@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();
}
示例10: getSizeOfBitmap
import android.graphics.Bitmap; //导入方法依赖的package包/类
@SuppressLint("NewApi")
public int getSizeOfBitmap(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 {
// Estimate for earlier platforms.
return bitmap.getWidth() * bitmap.getHeight() * 4;
}
}
示例11: setPhoto
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* Set or update UserAccount photo. If photo is over the size limit, it attemps three times to
* shrink the filesize by scaling the photo down.
* @param photo Bitmap
*/
public void setPhoto(Bitmap photo) {
if (photo != null) {
Log.i("HabitUpDEBUG", "Photo is " + String.valueOf(photo.getByteCount()) + " bytes.");
if (photo.getByteCount() > HabitUpApplication.MAX_PHOTO_BYTECOUNT) {
for (int i = 0; i < 3; ++i) {
photo = resizeImage(photo);
Log.i("HabitUpDEBUG", "Resized to " + String.valueOf(photo.getByteCount()) + " bytes.");
if (photo.getByteCount() <= HabitUpApplication.MAX_PHOTO_BYTECOUNT) {
break;
}
}
}
if (photo.getByteCount() <= HabitUpApplication.MAX_PHOTO_BYTECOUNT) {
this.photo = photo;
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOS);
this.encodedPhoto = Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
} else {
throw new IllegalArgumentException("Image file must be less than or equal to " +
String.valueOf(HabitUpApplication.MAX_PHOTO_BYTECOUNT) + " bytes.");
}
} else {
this.photo = null;
this.encodedPhoto = null;
}
}
示例12: setPhoto
import android.graphics.Bitmap; //导入方法依赖的package包/类
/**
* Set or update UserAccount photo
* @param photo Bitmap (photo to set for event)
*/
public void setPhoto(Bitmap photo) {
if (photo != null) {
// Log.i("HabitUpDEBUG", "Photo is " + String.valueOf(photo.getByteCount()) + " bytes.");
if (photo.getByteCount() > HabitUpApplication.MAX_PHOTO_BYTECOUNT) {
for (int i = 0; i < 3; ++i) {
photo = resizeImage(photo);
// Log.i("HabitUpDEBUG", "Resized to " + String.valueOf(photo.getByteCount()) + " bytes.");
if (photo.getByteCount() <= HabitUpApplication.MAX_PHOTO_BYTECOUNT) {
break;
}
}
}
if (photo.getByteCount() <= HabitUpApplication.MAX_PHOTO_BYTECOUNT) {
this.photo = photo;
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOS);
this.encodedPhoto = Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
} else {
throw new IllegalArgumentException("Image file must be less than or equal to " +
String.valueOf(HabitUpApplication.MAX_PHOTO_BYTECOUNT) + " bytes.");
}
} else {
this.photo = null;
this.encodedPhoto = null;
}
}
示例13: getBitmapSize
import android.graphics.Bitmap; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.KITKAT)
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
return bitmap.getByteCount();
}
return bitmap.getRowBytes() * bitmap.getHeight(); // earlier version
}
示例14: sizeOf
import android.graphics.Bitmap; //导入方法依赖的package包/类
@Override
protected int sizeOf(final LibraryItem key, final Drawable value) {
// All LibraryItems use BitmapDrawable for the artwork
final Bitmap artworkBitmap = ((BitmapDrawable) value).getBitmap();
return artworkBitmap.getByteCount();
}
示例15: sizeOf
import android.graphics.Bitmap; //导入方法依赖的package包/类
@Override
protected int sizeOf(Uri key, Bitmap bitmap) {
// The cache size will be measured in bytes rather than number of items.
return bitmap.getByteCount();
}