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

4

9

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.

Ceria answered 2/1, 2011 at 17:52 Comment(0)
S
19

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);
Sibilate answered 2/1, 2011 at 18:10 Comment(1)
Is that .../test.png a typo then?Fathom
C
4
InputStream in = new FileInputStream(file);
try {
    Bitmap bitmap = BitmapFactory.decodeStream(in);
    File tmpFile = //...;
    try {
        OutputStream out = new FileOutputStream(tmpFile);
        try {
            if (bitmap.compress(CompressFormat.JPEG, 30, out)) {
                { File tmp = file; file = tmpFile; tmpFile = tmp; }
                tmpFile.delete();
            } else {
                throw new Exception("Failed to save the image as a JPEG");
            }
        } finally {
            out.close();
        }
    } catch (Throwable t) {
        tmpFile.delete();
        throw t;
    }
} finally {
    in.close();
}
Ceria answered 2/1, 2011 at 20:31 Comment(0)
H
2

@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();
Hols answered 18/7, 2016 at 20:57 Comment(0)
B
1

Using Kotlin to save a file in path to tmpPath:

Files.newInputStream(path).use { inputStream ->
    Files.newOutputStream(tmpPath).use { tmpOutputStream ->
        BitmapFactory
            .decodeStream(inputStream)
            .compress(Bitmap.CompressFormat.JPEG, 30, tmpOutputStream)
    }
}

Edit: make sure you check for the possibility of the decode failing (and returning null) and also that compress actually worked (boolean return type).

    val success: Boolean = Files.newInputStream(path).use { inputStream ->
        Files.newOutputStream(tmpPath).use { tmpOutputStream ->
            BitmapFactory
                .decodeStream(inputStream)
                ?.compress(Bitmap.CompressFormat.JPEG, config.qualityLevel, tmpOutputStream)
                ?: throw Exception("Failed to decode image")
        }
    }

    if (!success) {
        throw Exception("Failed to compress and save image")
    }
Borst answered 8/2, 2019 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.