AggelosK
AggelosK

Reputation: 4341

Decoded bitmap results in larger size

I am trying to store a bitmap (that i have previously read from a file) after decoding it in a preferable size using BitmapFactory.Options.inSampleSize. The problem is that the file size of the stored bitmap is at least double the size of the original file. I have searched a lot and could not find how i can deal with this, since i don't want it to happen for memmory efficiency (later i reuse the stored bitmap). Here is my method that does what i describe:

private Bitmap decodeFileToPreferredSize(File f) {
    Bitmap b = null;
    try {
        // Decode image size
        Log.i("Bitmap", "Imported image size: " + f.length() + " bytes");
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(f.getAbsolutePath(), o);

        //Check if the user has defined custom image size
        int scale = 1;
        if(pref_height != -1 && pref_width != -1) {

            if (o.outHeight > pref_height || o.outWidth > pref_width) {

                scale = (int) Math.pow(
                        2,
                        (int) Math.round(Math.log(pref_width
                                / (double) Math.max(o.outHeight, o.outWidth))
                                / Math.log(0.5)));
            }
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        b = BitmapFactory.decodeFile(f.getAbsolutePath(), o2);
        String name = "Image_" + System.currentTimeMillis() + ".jpg";
        File file = new File(Environment.getExternalStorageDirectory(), name);
        FileOutputStream out = new FileOutputStream(file);
        b.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.close();
        Log.i("Bitmap", "Exported image size: " + file.length() + " bytes");

    } catch (Exception e) {
        b = null;
    }

    return b;
}

UPDATE I saw the densities on the two image files. The one that came from camera intent has 72 dpi density for width and height. The image file created from my above method has 96 dpi density for width and height. This explains why a 0.5 MByte image that came form the camera is resized in approximatelly 2.5 MByte with my above method since the rescale factor is (96/72) * 2 ~= 2.5. For some reason, the bitmap i create does not take the density of the image that came from the camera. I tried to set the density with all variation of BitmapFactory.Options.inDensity but no luck. Also i tried to change the bitmap density with bitmap.setDensity(int dpi); but still no effect. So, my new question is if there is a way to define the density of the bitmap when the image is stored.

Thanks in advance.

Upvotes: 2

Views: 2740

Answers (3)

Roy Shilkrot
Roy Shilkrot

Reputation: 3548

You are correct that the problem is in a density change.

This thread revealed a solution: BitmapFactory returns bigger image than source

Before decoding also disable inScaled:

options.inSampleSize = 2; //or however you desire, power of 2
options.inScaled = false;

This will keep the density from changing and you will see the decrease in size you were looking for.

Upvotes: 2

Warpzit
Warpzit

Reputation: 28162

I had a similar issue. When I downloaded images from web they used more space on the SD than they did when downloaded to my PC from browser. I think the issue is simply that BitmapFactory saves the images in a non optimzed format of some sort.

My workaround was to use following instead of the bitmapfactory:

    try {
        try {
            is = yourinputstream;
            // Consider reading stream twice and return the bitmap.

            os = youroutputstream;

            byte data[] = new byte[4096];
            int count;
            while ((count = is.read(data)) != -1) {
                os.write(data, 0, count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(is != null) {
                is.close();
            }
            if(os != null) {
                os.close();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

Upvotes: 2

Bigflow
Bigflow

Reputation: 3666

When BitmapFactory.Options.inSampleSize = 0.5, it does 100% / 0.5 = 200%.
What you want I think is BitmapFactory.Options.inSampleSize = 2, it does 100% / 2 = 50%

Upvotes: 0

Related Questions