0

My problem is the performance of reading a internal file. It's really slow. I didnt measure the time, but IMHO it is really slow. 2-3 seconds for reading a file like this:

private static String readJsonFile(String type, Context context) {
    int ch;
    StringBuffer sb = new StringBuffer("");
    FileInputStream fis = null;
    try {
        fis = context.openFileInput(context.getResources().getString(R.string.class.getField("filename_" + type).getInt(null)));
        while ((ch = fis.read()) != -1) {
            sb.append((char)ch);
        }
    } catch (NotFoundException | IllegalAccessException | IllegalArgumentException | NoSuchFieldException | IOException e) {
        e.printStackTrace();
    }
    String jsonString = new String(sb);
    return jsonString;
}

The data gets written in the splash screen of the app, which uses a AsyncTask to download 5 different json input strings.

I wonder if it would be quicker to save the data in the shared preferences in order to access the data in all activities. So I would not need to make a file access operation. But isnt the SharedPrefs also a file access operation, reading an xml file?

Or should I call all functions, which need to access the globally available json data by reference function parameters? Dos this work?

To cut a long story short: I need a pattern/schema on how to store the 5 different JSON data Strings during the lifecycle of my app, so that each and every activity can access them, without doing a file operation, or a database request?

Is there somekind of app-cache to do this?

EDIT: Another idea in my mind would be to use HttpResponseCache and start the asyncActivity downloading the data (in this case from cache). But still isnt this also a file reading operation?

Thanks

Klaus
  • 264
  • 2
  • 15
  • I found somthing which maybe answers my question: http://stackoverflow.com/questions/2676280/good-way-to-cache-data-during-android-application-lifecycle But still any other suggestions? – Klaus Apr 15 '14 at 05:41

1 Answers1

0

I ended up using a extended Application class, holding my global Variables. This is fast, and I do not have to read a file all the time. Also I do not have to use as many parameters in the fragment constructors.

Klaus
  • 264
  • 2
  • 15