bitmap.compress(Bitmap.CompressFormat.PNG, 0, fOut) making image size bigger than original
Asked Answered
J

2

19

I want to compress the image taken from a camera to png format to make them smaller in size, so I'm using this code:

compressedPictureFile = new File(imagePath);
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
FileOutputStream fOut = new FileOutputStream(compressedPictureFile);
boolean compressed = bitmap.compress(Bitmap.CompressFormat.PNG, 0, fOut);
fOut.flush();
fOut.close();

The problem is that compressedPictureFile is actually bigger that the original image (from 1 Mb to 6Mb)

What am I missing? And is this the best way to reduce the size of an image?

Thanks

Jannette answered 23/4, 2013 at 19:2 Comment(0)
M
29

The images taken with the camera are most likely stored in the jpg-format, which is lossy, but is relatively good for images with lots of colors (such as photographs).

When you compress the bitmap using your method you save it as a png. The png compression is lossless, but can achieve pretty small filesizes when there are few colors (such as in certain logotypes or other graphics). When the amount of colors and complexity in a png-file increases, so will the file size (this is why the camera saves a jpg - the quality/filesize ratio is much better than png for most photographs).

So, if you want to decrease the filesize of the photographs, use jpg compression and experiment with the quality-parameter. You might also want to decrease the resolution of the images, since this will save lots of space (a file with 50% resolution will be approx. 25% in data-size).

Magner answered 23/4, 2013 at 19:21 Comment(6)
is the quality parameter the same as resolution ? how can i decrease the resolution ?Jannette
The quality-paramter affects how well preserved the colors will be when using jpg-compression. It does not have any effect when outputting a png. You can read a little about the compress-method here: developer.android.com/reference/android/graphics/…Magner
There are multiple tutorials on decreasing image size out there, but the easiest way is to simply create a new, smaller bitmap through Bitmap.createScaledBitmap: developer.android.com/reference/android/graphics/…Magner
if you are using PNG format then it will not compress your image because PNG is a lossless format. use JPEG for compressing your imageAffinity
@Adeel, PNG uses lossless compression.Magner
@Magner dear same thing i mentioned in my above comment :)Affinity
S
0

it work fine on my app

    public static String makeScreen(View view) throws Exception {
    String filename = "file.png";
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap b = view.getDrawingCache();
    FileOutputStream file = null;

    try {
        file = new FileOutputStream(Environment.getExternalStorageDirectory().toString() + "folder/" + filename);
        if (file != null) {
            b.compress(Bitmap.CompressFormat.PNG, 98, file);
            file.close();
        }
        view.destroyDrawingCache();
    } catch (Exception e) { view.destroyDrawingCache(); }
}
Stony answered 5/2, 2018 at 10:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.