1

I'm working with bitmap and have some problems need to help: My app works as below:

  1. Load JPG image file(1) from SDcard to bitmap1
  2. Save this bitmap1 to new JPG file(2).
  3. Load new JPG image(2) file to bitmap2
  4. Save bitmap2 to new JPG file(3) ....
  5. .... repeat again and again

Now I can load/save bitmap to file, but problem is quality of image reduces after load/save. So if I do load/save stuff for 10 times, so my image become ugly. This is my code:

private void saveBitmapToFile(String imgPath) {

    Log.e("Filename-----------------", imgPath);

    // Decode image file to bitmap
    BitmapFactory.Options options = new BitmapFactory.Options();
    // options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);

    // Get filename
    long currentMili = System.currentTimeMillis();
    currentName = currentMili + "";
    String filePath = FOLDER_PATH + currentMili + ".jpg";

    // Save bitmap to new file
    try {

        File file = new File(filePath);
        FileOutputStream fOut = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {

        e.printStackTrace();
    }
}
  • I just [asked a similar question](https://stackoverflow.com/questions/52729431/why-doesnt-imageio-preserve-jpeg-data-when-repeatedly-loading-and-saving) where I want to know the _reason_ this happens. Maybe someone knows. – Roland Illig Oct 09 '18 at 21:22

1 Answers1

1

You're re-compressing a lossy file format. You're going to get image artifacts doing that. If you need to do this for some reason, use a lossless format like png.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thanks for your help. But I can't use PNG instead because I must to add some information to image's exif(PNG does not support). – user3579893 Jun 09 '14 at 04:15