0

I have an app which gets data from Parse.com and uses in 2 sections, one in a listView and second in a grid view.

So I want to cache this data(Strings,bitmaps),to work offline, whenever the device won't have internet so it loads from cache. And whenever my app closed and opened again, it should update the cached data if there is internet. During the update if I have the same data in my cache it shouldn't create it again, it has to only create the new ones and update the views.

What is the best way to do this in android ?. Thanks.

Shant11
  • 29
  • 7
  • 1
    you can use `shared preference in android` for this because it keep data in your application cache, and it secures data. for more info follow this link-[http://stackoverflow.com/questions/23024831/android-shared-preferences-example](http://stackoverflow.com/questions/23024831/android-shared-preferences-example) – sud Nov 23 '15 at 07:08
  • @sud.Thank you for your comment, I think it's not recommended to save bitmaps in shared preference, am I wrong ?. Thanks. – Shant11 Nov 23 '15 at 07:15
  • yes right i thought you want to save the string values... – sud Nov 23 '15 at 07:19
  • No not only Strings...@sud. – Shant11 Nov 23 '15 at 07:21
  • but practicaly you can save bitmap in shared pref but its not good practice – sud Nov 23 '15 at 07:22
  • Bitmaps could be written to and retrieved from your app's local data directory. – mjp66 Nov 23 '15 at 07:44
  • How you can do that ? @mjp66 – Shant11 Nov 23 '15 at 07:47

3 Answers3

1

For caching a data coming form Webservice

for Get a data u can use Volley or retrofit after getting an data you can set it to user desired attributes for caching the data both library giving the caching option for saving an data try to use that library.

santoXme
  • 802
  • 5
  • 20
  • Thank you for your comment, I'm getting the data already and in easy way with parse.com api, I just don't know where is the best way to cache it. But yea I will try it. @antosh rawat – Shant11 Nov 23 '15 at 07:27
0

For String / Text :

  1. If your data amount is not so much , you can use SharedPreference

  2. If you have huge Data you should use SQlite

For Image :

  1. Picasso -> http://square.github.io/picasso/

  2. Universal Image Loader -> https://github.com/nostra13/Android-Universal-Image-Loader

Zahidul Islam
  • 3,180
  • 1
  • 25
  • 35
0

try this code it will help-

File cacheDir = getBaseContext().getCacheDir();
File f = new File(cacheDir, "pic");

try {
    FileOutputStream out = new FileOutputStream(
            f);
    pic.compress(
            Bitmap.CompressFormat.JPEG,
            100, out);
    out.flush();
    out.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Intent intent = new Intent(
        AndroidActivity.this,
        OpenPictureActivity.class);
startActivity(intent);

and then in the new activity-

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.open_pic_layout);
    File cacheDir = getBaseContext().getCacheDir();
    File f = new File(cacheDir, "pic");     
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(f);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(fis);

    ImageView viewBitmap = (ImageView) findViewById(R.id.icon2);
    viewBitmap.setImageBitmap(bitmap);
sud
  • 505
  • 1
  • 4
  • 12
  • Thank you for your answer, so you recommend me to write it on the local disk, am I right ? .@sud – Shant11 Nov 23 '15 at 07:29
  • Okay, do you need to clean this file or not ? @sud – Shant11 Nov 23 '15 at 07:35
  • read this you will be get more clear about `cachedir` [http://stackoverflow.com/questions/13731203/why-to-use-getcachedir-on-android](http://stackoverflow.com/questions/13731203/why-to-use-getcachedir-on-android) – sud Nov 23 '15 at 07:38