What is the best way to store images locally in android?

Viewed 2553

I want to create an offline app in which the user can add images with image detail. I want to as that what is the best way to store image offline?

Should I use SQLite blob format, SQLite base64 encoded String or should I store it in internal storage or Should I store It in shared Preference. Please help me, If you know any other way then please mention it.

2 Answers

Internal storage is best of all. if you want to keep it private ,change format of image and then save otherwise save as it is

To save image locally use below code :

private String saveToInternalStorage(Bitmap bitmapImage){
    ContextWrapper cw = new ContextWrapper(getApplicationContext());
     // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath=new File(directory,"profile.jpg");

    FileOutputStream fos = null;
    try {           
        fos = new FileOutputStream(mypath);
   // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
    } catch (Exception e) {
          e.printStackTrace();
    } finally {
        try {
          fos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
    } 
    return directory.getAbsolutePath();
}
Related