How can you save and load files in android?

edited November 2013 in Android Mode

It works in java mode, and the program runs in android mode, but doesn't seem to save the data.

Answers

  • Have you enabled the permission WRITE_EXTERNAL_STORAGE? (Go to Android > Sketch Permissions.)

    If you've already done that... then can you provide the code that doesn't work?

  • edited November 2013

    calsign , I have enabled the permission WRITE_EXTERNAL_STORAGE It seems to me that loadStrings() works but saveStrings() doesn't, the program runs but the score is not saved. And by the way it DOES work in java mode. I have put a text file in the program folder called "score.txt". It starts out with the number 0 typed in it. Here is the code that relates to the data being loaded and saved:

    // above setup and draw 
    int score; 
    boolean scoreUp = false;
    
    // in setup 
    try {
      String[] scoreData = loadStrings("score.txt");
      score = int(scoreData[0]);
    }
    catch(ArrayIndexOutOfBoundsException e) {
      score = 0; 
    }
    
    // in draw
    if(scoreUp==true) {
      score++;
      scoreUp=false;
    }
    
    // on exit
    void exit() {
      String[] savedScore = {str(score)};
      saveStrings("score.txt",savedScore);
      super.exit();
    }
    
  • Answer ✓

    Firstly, I have been successful in saving files to absolute paths - that is, not relative paths such as the one that you attempt to use, but rather something like this:

    \\sdcard\\MyFile.txt
    

    I have not experimented much beyond this... for example, checking to see if the user's device has an SD card available. It might be possible to use relative paths, but I haven't been able to.

    Using Processing's Table class, however, is a different story. I have been able to save and load data with a relative path (this is what I used for the high-score table in GeoTecha). In many cases, this may be better suited to storing the data anyway.

    Also, it is important to mention that exit() may or may not actually be called properly... you never really know. A more effective approach (in my opinion, at least) is to save whenever you update the data. Depending on the situation, this may be ineffective; in GeoTecha, I save only after a game ends... but from what I can see in your code, this may not work. If you want to save when the application stops, it is probably best to use onStop() and onDestroy(). These are two native Android methods that tend to work (for me, at least). For example:

    //public is necessary
    
    @ Override
    public void onStop() { //This should be called when the app closes
      //Save stuff
      super.onStop();
    }
    
    @ Override
    public void onDestroy() { //This might be called when the app is killed
      //Save stuff
      super.onDestroy();
    

    }

  • First, I can't get the absolute path to work, the error says that the file is missing. Second, I like the sound of using the Table class, but I have not seen or hear of that before so if you could give me an example it would help a lot! p.s. I liked GeoTecha a lot, it was a very cool idea.

  • The Table class is a utility added in Processing 2.0 used for reading and writing CSV and TSV files. There is also a bit on it in the Data Tutorial.

  • OK, thanks for all your help!

Sign In or Register to comment.