How to save a JPEG image on Android with a custom quality level

Daniel Trebbien picture Daniel Trebbien · Jan 2, 2011 · Viewed 31k times · Source

On Android, how do I save an image file as a JPEG at 30% quality?

In standard Java, I would use ImageIO to read the image as a BufferedImage, then save it as a JPEG file using an IIOImage instance: http://www.universalwebservices.net/web-programming-resources/java/adjust-jpeg-image-compression-quality-when-saving-images-in-java. It appears, however, that Android lacks the javax.imageio package.

Answer

Phyrum Tea picture Phyrum Tea · Jan 2, 2011

You can store your bitmap in the JPEG format by calling compress and setting the second parameter:


    Bitmap bm2 = createBitmap();
    OutputStream stream = new FileOutputStream("/sdcard/test.jpg");
    /* Write bitmap to file using JPEG and 80% quality hint for JPEG. */
    bm2.compress(CompressFormat.JPEG, 80, stream);