Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 357
Daniel Trebbien Avatar asked Jan 02 '11 17:01

Daniel Trebbien


1 Answers

@Phyrum Tea is good only do not forget close everything

InputStream in = new FileInputStream(context.getFilesDir() + "image.jpg");
Bitmap bm2 = BitmapFactory.decodeStream(in);
OutputStream stream = new FileOutputStream(String.valueOf(
        context.getFilesDir() + pathImage + "/" + idPicture + ".jpg"));
bm2.compress(Bitmap.CompressFormat.JPEG, 50, stream);
stream.close();
in.close();
like image 164
Ezequiel García Avatar answered Sep 19 '22 12:09

Ezequiel García