edited July 2018 in Android Mode

I am having trouble using the saveStrings() function. I am using it in APDE. But I have tried the same code on processing 3.2.1.

String sentence = "This is a sentence";
String[] save;
String[] load;

void setup(){
  size(displayWidth, displayHeight);

  save = split(sentence, " ");
  text(save[0], width/2, height*0.2);
  text(save[1], width/2, height*0.4);
  saveStrings("strings.txt", save);
  load = loadStrings("strings.txt");
  text(load[2], width/2, height*0.6);
  text(load[3], width/2, height*0.8);

This should output the words "This is a sentence" down the middle of the window. However it returns an error saying that the index called is longer than the string length, for text(load[2], width/2, height*0.6);. Meaning the the variable "load" didn't load in anything from "strings.txt". I know this is an issue with saveStrings() because when I open strings.txt and put in 1, 2, 3, 4 on different lines, the code ouputs "This is 3 4" down the middle. Also when I check strings.txt it doesn't have the String[] saved. I've tried putting strings.txt in different locations but it still doesn't work.



  • Answer ✓

    I don't have experience with the APDE but I can make a suggestion to use sketchPath. In short, Android mode follows certain standard. Currently loadStrings look inside the data folder. Where is this folder? It is a space referred as the assets of an Android project. This space is read only. You should be able to call loadStrings, loadBytes, LoadImage, etc. Writing is a bit trickier however. From my experience, if you do

    String fullPath = sketchPath("")+filename;

    then you should be able to call

    saveStrings(fullPath , saveData);
    load = loadStrings(fullPath );

    Where is the folder of sketchPath in Android? From what I understand, it is not in the assets folder but one folder down the path, but private to your application. This space is not accessible via some folder manager app in Android. (I will guess if your phone is rooted, then you should be able to see it).

    There other ways to store data. Using the external storage would require access permissions as you might know.

    Notice however that I am not sure if the APDE behaves the same way as Android Mode. I would suspect it does as there is no point to have two different source codes. The best thing you could do is to try to setup a simple test, just as what you did for this case above. Also, I suggest to check the data retrieved is not null and that you invoke those functions at two different times and no one call right after the other, meaning saving followed by a loading. Instead, you can do something like this:

    void mousePressed(){
      if(mouseY>height/2)  //Tapped upper half of the screen
         //load your file
        //save your file

    This way, you ensure you are testing loading and saving and you don't have to worry conditional racing btw processes.

    By the way, there is a new forum. Better if you continue any other conversation over there.


Sign In or Register to comment.