2

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.

AggelosK
  • 4,313
  • 2
  • 32
  • 37

3 Answers3

2

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();
    }
Warpzit
  • 27,966
  • 19
  • 103
  • 155
  • Thank you for your answer! I was hoping to avoid this workaround since i want to decode the image in a custom format that the user may want. But i'll use it as a final resort if i cannot find anything else. – AggelosK Jul 12 '12 at 10:39
  • I see the problem. You could use this for the format the camera use and then bitmapfactory if the user wants it in another format. – Warpzit Jul 12 '12 at 13:14
2

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.

Community
  • 1
  • 1
Roy Shilkrot
  • 3,079
  • 29
  • 25
0

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%

Bigflow
  • 3,616
  • 5
  • 29
  • 52
  • 1
    Thank for your answer but in `BitmapFactory.Options.inSampleSize` documantation states that `Any value <= 1 is treated the same as 1.` Also, i know that i have to use a >1 value `BitmapFactory.Options.inSampleSize` preferably a power of 2, but the same thing happens. – AggelosK Jul 12 '12 at 09:02
  • @Angelo what do you get, when you `System.out.println("scale = "+scale);` ? – Bigflow Jul 12 '12 at 10:14
  • I have updated my question. I believe the problem is eventually in the density. Thanks. – AggelosK Jul 12 '12 at 10:28