3

I am storing the bitmap in SD card while retrieving loosing their quality. how can i resolved this issue. Below is my code to store the bitmap in Sd card.

public void saveExternalPrivateStorage(File folderDir, String fname,
        Bitmap bitmap) {

    File file = new File(folderDir, fname);

    if (file.exists()) {
        file.delete();
    }
    if ((folderDir.mkdirs() || folderDir.isDirectory())) {
        try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 0, out);
            out.flush();
            out.close();
            System.out.println("Stored");
            System.out.println(file.getAbsolutePath());
            System.out.println("Stored");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

invoking this method:

File folderDir= new File(getActivity().getExternalFilesDir("myfolder"),"/images");
storeImagesIndevices.saveExternalPrivateStorage(folderDir,filename,imgBitmapUrls.get(i));

Retrieving from SD card:

Bitmap bitmap2 = BitmapFactory.decodeFile(folderDir+"/"+data.getTid());
        dbimgBitmapUrls.put(data.get_tid()-1, bitmap2);

Thanks a lot...

Roman C
  • 49,761
  • 33
  • 66
  • 176
user3154663
  • 291
  • 1
  • 2
  • 18

2 Answers2

3
bitmap.compress(Bitmap.CompressFormat.JPEG, 0, out);

JPEG is a lossy compression format and you've setting 0% as the quality parameter. Something around 70% is usually a good compromise between file size and image quality:

bitmap.compress(Bitmap.CompressFormat.JPEG, 70, out);

Or use a lossless format such as PNG where the quality parameter does not matter:

bitmap.compress(Bitmap.CompressFormat.PNG, 0, out);

Can store the bitmap in SQLite? is it good approach. In my case number of images not that much

You can but you shouldn't. A better approach is to store the images as files and just store the path in database.

laalto
  • 150,114
  • 66
  • 286
  • 303
3

You should use a higher compression quality:

 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);

The second parameter to compress is a 0-100 number which marks the tradeoff between quality and file size:

  • 0 = smallest file size, worst quality
  • 100 = largest file size, best quality

Probably you will want an intermediate value, just play a bit until you find a satisfactory value for your application. Also refer to compress() javadoc.

If it is the case, you could also use a lossless format (like PNG) instead of JPEG.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
  • Can store the bitmap in SQLite? is it good approach. In my case number of images not that much . @Stefano Sanfilippo – user3154663 Jan 11 '14 at 12:14
  • @user3154663 storing in SQLite does not change the quality loss problem, it would be just like saving in another folder. Besides, use SQLite private DB only for configuration, history and the like. User media should stay accessible to user, in `/sdcard`, unless you have very special needs. – Stefano Sanfilippo Jan 11 '14 at 12:18