Bitmap.compress Results In Too Large Of File
I have an app that needs to resize an image and then save it to jpg. My test image is a photo with very smooth gradients in the sky. I'm trying to save it to jpeg after a resize
Solution 1:
Try this code from http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
In short, you have 2 static methods
the first for calculating the new size of the image
publicstaticintcalculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of imagefinalintheight= options.outHeight;
finalintwidth= options.outWidth;
intinSampleSize=1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and widthfinalintheightRatio= Math.round((float) height / (float) reqHeight);
finalintwidthRatio= Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee// a final image with both dimensions larger than or equal to the// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
second for loading the scaled size image into memory:
publicstatic Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensionsfinal BitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
and You can call this function like this:
Bitmapb= decodeSampledBitmapFromResource(getResources(), R.id.myimage, 128, 128));
You can modify the second method to take input parametar String (the path if the image is file on sdcard) instead of resource, and instead of decodeResource use:
BitmapFactory.decodeFile(path, options);
I always use this code when loading big images.
Post a Comment for "Bitmap.compress Results In Too Large Of File"