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

The following are Jave code examples for showing how to use getByteCount() 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: CIA   File: ImageUtilities.java   View Source Code Vote up 7 votes
/**
 * 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;
}
 
Example 2
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 3
Project: HDImageView   File: ImageCache.java   View Source Code Vote up 6 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 4
Project: SuperSelector   File: ImageCache.java   View Source Code Vote up 6 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(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();
	}
 
Example 5
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 6
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 7
Project: CIA   File: CreateHabitEventActivity.java   View Source Code Vote up 5 votes
/**
 * 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();
        }
    }
}
 
Example 8
Project: CIA   File: ImageUnitTests.java   View Source Code Vote up 5 votes
/**
 * 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);
}
 
Example 9
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 10
Project: GitHub   File: AnimatedDrawableUtil.java   View Source Code Vote up 5 votes
@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;
  }
}
 
Example 11
Project: HabitUp   File: UserAccount.java   View Source Code Vote up 5 votes
/**
 * 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;
    }
}
 
Example 12
Project: HabitUp   File: HabitEvent.java   View Source Code Vote up 5 votes
/**
     * 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;
        }
    }
 
Example 13
Project: AndroidBasicLibs   File: BlockImageLoader.java   View Source Code Vote up 5 votes
@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
}
 
Example 14
Project: Mixtape   File: ToolbarHeaderTestHarness.java   View Source Code Vote up 4 votes
@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();
}
 
Example 15
Project: Mixtape   File: RecyclerViewBodyTestHarness.java   View Source Code Vote up 4 votes
@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();
}
 
Example 16
Project: SOS-The-Healthcare-Companion   File: ContactPictureCache.java   View Source Code Vote up 4 votes
@Override
protected int sizeOf(Uri key, Bitmap bitmap) {
    // The cache size will be measured in bytes rather than number of items.
    return bitmap.getByteCount();
}
 
Example 17
Project: AndroidBackendlessChat   File: VolleyUtils.java   View Source Code Vote up 4 votes
@Override
protected int sizeOf(String key, Bitmap bitmap) {
    int size = bitmap.getByteCount() / 1024;
    return size;
}
 
Example 18
Project: Mixtape   File: CoordinatedMixtapeContainerTestHarness.java   View Source Code Vote up 4 votes
@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();
}
 
Example 19
Project: Simpler   File: BitmapUtils.java   View Source Code Vote up 4 votes
/**
 * 得到上传图片的Bitmap
 *
 * @param source 图片路径
 * @return
 */
public static Bitmap getUploadImage(String source) {
    File file = new File(source);
    if (!file.exists() || !file.isFile()) {
        return null;
    }

    Bitmap bitmap = BitmapFactory.decodeFile(source);

    if (source.toLowerCase().endsWith(".gif")) {
        Log.w(TAG, "上传图片是GIF图片,上传原图");
        return BitmapFactory.decodeFile(source);
    }
    BaseSettings.sSettings = SettingsUtil.readSettings(BaseConfig.sUid, false);
    int type = BaseSettings.sSettings.uploadQuality;
    int size = bitmap.getByteCount();
    int sizeM;
    switch (type) {
        case PicQuality.ORIGINAL:
            sizeM = 5;
            if (size < sizeM * 1024) {
                // 小于sizeM
                return bitmap;
            } else {
                Bitmap image = getImage(bitmap, sizeM);
                Log.d("Image", image.getByteCount() + "字节");
                return image;
            }
        case PicQuality.MIDDLE:
            sizeM = 4;
            if (size < sizeM * 1024) {
                // 小于sizeM
                return bitmap;
            } else {
                return getImage(bitmap, sizeM);
            }
        case PicQuality.THUMBNAIL:
            sizeM = 3;
            return getImage(bitmap, sizeM);
        default:
            return null;
    }
}
 
Example 20
Project: simple-share-android   File: ThumbnailCache.java   View Source Code Vote up 4 votes
@Override
protected int sizeOf(Uri key, Bitmap value) {
    return value.getByteCount();
}