Search code examples
androidbitmapjpegoutputstreamimage-compression

bitmap.compress destroys the picture quality


I'm using an app to get the gps location and draw it as a circle on a bitmap then save it to proceed, so I need repetitively to read and save the file. But unfortunately when I save the file and read it, the file is damaged after some iterations...!
the code:

File output = new File(tmpDirectory, "map.jpg");
    try {
        OutputStream outputStream = new FileOutputStream(output);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream.flush();
        outputStream.close();
    } catch (Exception ex) {
        Message("error!");
    }

    directory = tmpDirectory;//updating directory to load the manipulated image
    readFile(directory + "map.jpg", false);//setting the image view new image

image included:picture after iterations image included: main image


Solution

  • JPEG uses lossy compression. That means with each iteration you will lose some quality. You should use loseless format like PNG if you want to preserve it.

    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);