It is very crucial for some apps to be able to run even offline, e.g. news etc, therefore you will learn it in Android internal storage tutorial. So you should be able to cache some articles because the news in the morning is still very recent for you to read in the afternoon while there is no internet in the afternoon, for example.

In Android, there are some ways to do that. If your data amount is small, you might use SharedPreferences. Or use local SQLite for a complex data structure. How about the middle one? Then use internal storage file. Some pictures are best to store in the external one, but there is some good library for that, for example, Universal Image Loader or Picasso. But a for lightweight JSON text or ArrayList data for ListView, it is better to use the internal storage file.

For me, my best concept is DRY (Don’t Repeat Yourself). Why don’t you create one and use more later? Well, I have done one. I decided to create a library for internal storage cache myself, and I called FileCacher.

FileCacher is a simple library and easy to use because you need 4 lines of code to write and read. First, you create an instance of FileCacher. Like this:
FileCacher stringCacher = new FileCacher(MainActivity, “sometext.txt”);

Then, call stringCacher.writeCache(“some text”). And when you want to read it, call stringCacher.readCache(). But make sure to test it by invoking stringCacher.hasCache() before the readCache. That’s it.

DOWNLOAD SOURCE CODE

DOWNLOAD

Comments

comments


Warning: A non-numeric value encountered in /home/top12rev/public_html/kosalgeek.com/wp-content/themes/Newspaper/includes/wp_booster/td_block.php on line 352