Why Does Saving A Loaded Bitmap Increase It's Size?
I load a png file from the sdcard. I alter it in someway and then save it again. I have noticed that the saved file is much larger than the original. At first I thought it was beca
Solution 1:
You can control the size of your Bitmap by the compress factor (quality):
Bitmap.compress(Bitmap.CompressFormat format, int quality, FileOutputStream out);
100 = maximum quality, 0 = low quality.
Try like "10" or something to keep your image smaller. It could also be that you are loading a Bitmap that is in 16-Bit color and than later save it in 32-Bit color, increasing its size.
Check BitmapConfig.RGB_565, BitmapConfig.ARGB_4444 and BitmapConfig.ARGB_8888 respectively.
EDIT:
Here is how to load Bitmaps correctly:
publicabstractclassBitmapResLoader {
    publicstatic Bitmap decodeBitmapFromResource(Bitmap.Config config, Resources res, int resId, int reqWidth, int reqHeight) {
        final BitmapFactory.Optionsoptions=newBitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        options.inPreferredConfig = config;
        return BitmapFactory.decodeResource(res, resId, options);
    }
    privatestaticintcalculateInSampleSize(
                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;
    }
}
In code:
// Bitmap.Config.RGB_565 = 16 BitBitmapb= BitmapResLoader.decodeBitmapFromResource(Bitmap.Config.RGB_565, getResources(), width, height);
That way, you can control how your Bitmap is loaded into Memory (see Bitmap.Config).
Post a Comment for "Why Does Saving A Loaded Bitmap Increase It's Size?"