0

In my MainActivity onCreate() I want to populate a listview. This listview contains Strings and Bitmaps.

What's the best way to save these data to get them back when the application is restarted ?

Farhad
  • 12,178
  • 5
  • 32
  • 60
sampa
  • 535
  • 4
  • 27
  • How are you populating it initially? – Nic Robertson Mar 03 '16 at 20:44
  • Not yet. The listview is currently populated after a query submit. But I want to store the results (including images) of the last query result to show them after the application is restarted. – sampa Mar 03 '16 at 20:46
  • @sampa use SharedPreferences to save a simple data object. In your case you can save SQL Query there. – Sergey Shustikov Mar 03 '16 at 20:47
  • You'll want to serialise the data http://stackoverflow.com/questions/4118751/how-do-i-serialize-an-object-and-save-it-to-a-file-in-android – Nic Robertson Mar 03 '16 at 20:53
  • Its good approach to cache the images you're gonna use to load the listview. There are many libraries used to cache images. Picasso is one of them. You can also use universal imageloader. – Prudhvi Mar 03 '16 at 20:57

2 Answers2

1

There are different possibilities depending on how tightly you want to hang on to these files. The most reliable is the method getFilesDir(). Each application has its own private "files directory".

All the details are here: https://developer.android.com/training/basics/data-storage/files.html

The cache directory (getCacheDir()) is similar but the user can erase the cache contents so that's more for temporary files you don't want to hang onto.

Strings and bitmaps can both be saved to files. You can create your own name value mapping file to link them. Or use JSON objects, or many other things to organize your list of strings/bitmaps.

ssimm
  • 1,908
  • 3
  • 16
  • 36
1

There is my own cache solution:

public class MyCache {

private String DiretoryName;

public void OpenOrCreateCache(Context _context, String _directoryName){
    File file = new File(_context.getCacheDir().getAbsolutePath() + "/" + _directoryName);
    if(!file.exists()){
        file.mkdir();
    }
    DiretoryName = file.getAbsolutePath();
}

public void SaveBitmap(String fileName, Bitmap bmp){
    try {
        File file = new File(DiretoryName+ "/" + fileName);
        if(file.exists()){
            file.delete();
        }
        FileOutputStream Filestream = new FileOutputStream(DiretoryName + "/" + fileName);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        Filestream.write(byteArray);
        Filestream.close();
        bmp.recycle();
    }
    catch (Exception e){
        e.printStackTrace();
    }
}

public Bitmap OpenBitmap(String name){
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        File file = new File(DiretoryName+ "/" + name);
        if(file.exists()) {
            Bitmap bitmap = BitmapFactory.decodeFile(DiretoryName+ "/" + name, options);
            return bitmap;
        }
        else{
            return null;
        }
    }
    catch (Exception e){
        e.printStackTrace();
    }
    return null;
}

public void CleanCacheBitMap(){
    File file = new File(Diretorio);
    if(file.exists()){
        file.delete();
    }
}

}

And onCreate:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    cache = new MyCache();
    cache.OpenOrCreateCache(this, "TheFolderNameForOpenOrSaveInAppCache");
}

And for save on runtime:

cache.SaveBitmap("BitMapName", YourBitmap);

And openning on runtime:

Bitmap bmp = cache.OpenBitmap("BitMapName");

This solution save any bitmap, in particular folder in your app cache (internal storage).

Paulo Rodrigues
  • 723
  • 1
  • 5
  • 16
  • I orientated on your example and it worked very well. There were only a few major changes. Also I'm not recycling the bmp to keep it in memory for a while. Anyway thanks for pointing me to the right direction. – sampa Mar 05 '16 at 20:11