Android Mode saving and loading txt file

I’m having trouble saving and loading .txt files on android mode sketch. I have looked at past discussions about how to resolve the problem but it makes no sense to me.
What’s the difference in saving and loading .txt files on java mode and android mode? And how do you resolve the problem?

Any info helps @imaginativeCODE.

No problem guys I think I found a way to do it.

I found that if you put your “example.txt” inside a folder called data and put the data folder inside of your sketch folder then it loads up just fine.

p.s. this works with images and all other things you want to save and load. Also don’t write the “loadStrings” or “loadImage” like this - “loadStrings(“data/Example.txt”)”. The way I found to load and save is like this - “loadStrings(“Example.txt”)”. I don’t know why this works? But it does!

Here’s some example code below and images

PImage ExampleImage;
String[] ExampleText = new String[1];

void setup() {
  
  fullScreen();
  
  ExampleImage= loadImage("TestImage.png");
  ExampleText = loadStrings("TestStrings.txt");
  
}

void draw() {
  
  image(ExampleImage, 0, 0, width, height);
  fill(0);
  textSize(33);
  text(ExampleText [0], width/2 - textWidth(ExampleText [0])/2, height/2);
  
}

Inside of sketch folder
InSketchFolder

Inside of data folder
InsideDataFolder

Keep in mind that in future processing updates they might do it differently. I spent lots of time going threw old posts that didn’t work any more. I just had to figure it out by myself. Hope this helps some of you people trying to solve this.

1 Like

sorry, i not understand why you say

Also don’t write the “loadStrings” or “loadImage” like this - “loadStrings(“data/Example.txt”)”.

as i would think it is actually the good way?

// processing 3.4   / Java Mode
// https://processing.org/reference/loadStrings_.html
// https://processing.org/reference/saveStrings_.html

// pls make a subdir /data/
// and have the file there as
// /data/HalloWorld.txt
// if the file is not found you get that red warning
//
// The file "HalloWorld.txt" is missing or inaccessible, make sure the URL is valid
// or that the file has been added to your sketch and is readable.

// String filename1="HalloWorld.txt";
// you see, no path info given here, so the /data/ dir is assumed as default and works 
// but

String filename1="data/HalloWorld.txt";

// is actually a good way to do it.


// for saving a file we read:
// By default, this file is saved to the sketch's folder.
// but we don't want that there!
// to be more compatible to the loadStrings command
// the example should be changed to also using the /data/ dir.

String filename2="data/HalloWorld_copy.txt";

// funny thing on win7: the copy file is 2 bytes longer.

String[] lines;

//______________________________________________________
void get_it () {
  println("read from: "+filename1);
  lines = loadStrings(filename1);
  println("there are " + lines.length + " lines in "+filename1);
  for (int i = 0; i < lines.length; i++) {
    println(lines[i]); // println((i+1)+"_ "+lines[i]);
  }
}

//______________________________________________________
void save_it() {
  println("save to: "+filename2);
  saveStrings(filename2, lines);
}
//______________________________________________________
void setup() {
  get_it();
  save_it();
}

but as i state in the code, i only play processing 3.4 Java mode…
and not “Processing for Android”

Yeah I thought the same but in Processing Android Mode it works this way “loadStrings(“TestStrings.txt”)”, for some reason not sure why? The correct way that we do in Java Mode like this “loadStrings(“data/TestStrings”)”, will give you an error like this.

FATAL EXCEPTION: Animation Thread
Process: processing.test.examplesketch, PID: 5528
java.lang.IllegalArgumentException: File data/TestImage.png contains a path separator
	at android.app.ContextImpl.makeFilename(ContextImpl.java:1944)
	at android.app.ContextImpl.getFileStreamPath(ContextImpl.java:549)
	at android.content.ContextWrapper.getFileStreamPath(ContextWrapper.java:193)
	at processing.core.PSurfaceNone.getFileStreamPath(PSurfaceNone.java:317)
	at processing.core.PApplet.sketchPath(PApplet.java:5574)
	at processing.core.PApplet.createInputRaw(PApplet.java:5099)
	at processing.core.PApplet.createInput(PApplet.java:4931)
	at processing.core.PApplet.loadImage(PApplet.java:4026)
	at processing.test.examplesketch.ExampleSketch.setup(ExampleSketch.java:26)
	at processing.core.PApplet.handleDraw(PApplet.java:1846)
	at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:476)
	at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:516)

Basically it means that there was a path separator and it couldn’t load the image.

Keep in mind all this that I have only just started getting into Processing Android Mode now. Probably someone more experienced knows away to load up files with path separators. I think it might have something to do with “dataPath”, function call? But don’t trust my “dataPath”, theory.

Also if you put a folder named TestFolder inside of data and put TestImage inside of TestFolder and run this code “loadImage(“TestFolder/TestImage”)” to load up the image it works just fine. It must have something to do with the fact that all files and folders must be inside of data but don’t bet on it.

Thanks for the extra info this will help me in future projects.

Thank you “imaginativeCODE”, this was a very useful information for me. I was unaware of the fact that in android mode absolute paths for data files are not allowed and I have to admit that I still do not really understand the philosophy behind. However, Thanks to you I now know how to create an androide app using input and output files. I presume, saveStrings follows the same way?

I have found something out about saving files it works the same as loading but if the file was made before you run “Run on device” it won’t work. More details below.

loadStrings -
1: loading files made before “Run on device” - works
2: loading files made by the app - works

saveStrings -
1: saving onto files made before “Run on device” - error
2: saving onto files made by the app - works

Some code below for you to test run.

String[] SaveData = new String[1];
String[] LoadData = new String[1];
String[] FileData = {"My text is in here"};
String FileName = "File1.txt";

void setup() {
  fullScreen();
  
  SaveData[0] = "No data saved yet";
  LoadData[0] = "No data loaded yet";
  
}

void draw() {
  background(255);
  
  //Save data section and graphics
  fill(255, 0, 0);
  stroke(0);
  strokeWeight(2);
  rect(0, 0, width, height/2);
  fill(0);
  textSize(25);
  text("Click here to save", width/2 - textWidth("Click here to save")/2, height/4 - 25);
  text(SaveData[0], width/2 - textWidth(SaveData[0])/2, height/4 + 25);
  
  //Save if mouse is pressed below the half of the screen height
  if (mousePressed && mouseY < height/2) {
    //save to the name FileName or make a new txt called FileName inside of the data folder
    
    //comment this out to save the text in the string you set above
    FileData[0] = str(round(random(10)));
    SaveData = FileData;
    saveStrings(FileName, FileData);
    background(0);
    println("Saved");
    
  }
  
  //Load data section and graphics
  fill(0, 255, 0);
  stroke(0);
  strokeWeight(2);
  rect(0, height/2, width, height/2);
  fill(0);
  textSize(25);
  text("Click here to load", width/2 - textWidth("Click here to load")/2, height/2 + height/4 - 25);
  text(LoadData[0], width/2 - textWidth(LoadData[0])/2, height/2 + height/4 + 25);
  
  //Load if mouse is Pressed above half of the screen height
  if (mousePressed && mouseY > height/2) {
    //load the file from the directory of FileName which is inside of the data folder
    LoadData = loadStrings(FileName);
    background(0);
    println("Loaded");
    
  }
  
}

First run the code and see how it works when you understand try this below.
Make a folder called data inside the sketch folder.
Then make a .txt inside of the folder “data” and name it the same as the “String FileName” that is inside of the code above. Inside of the app load the file first then try and save it.
This helps visualize what I am saying for your self.

I suggest that if you wan’t to load image say for your character to draw in your app this works. But for storing data like where the character is and how much money he’s made, you have to make the file from inside the app like show in the example above. Hope this helps @imaginativeCODE

If you have any Problems understanding what I just said then let me know so it will help others understand better in the future too.

Hi,
I’m Hans (German), living in France, brand new user, my 1st post.
Installed on Win 10, processing 3.5.3 using the Android mode and the SDK downloaded through Processing IDE.
I’m trying to port a Java processing SW running on Raspberry to Android. OK, challenging for a Newby, but I’ve at least “some” programming background…:sunglasses:
Question1:
switching into Android mode makes the debugger option unavailable; there’s also no “butterfly”. Does this mean, debugging Android APPs must be done in Android Studio?
Question 2:
The following code lines seems not to work, although the file exists in the sketch’s data directory.
static final String CONFIG_FILE_VS = “settingsVS.txt”;
String[] lines = loadStrings(dataFile(CONFIG_FILE_VS));
Does this mean that the dataFile directive doesn’t work in Android mode?
Rgds
hk

@lve0200 ===
-answer 1= yes

-answer 2= yes (but anyway dataPath(CONFIG_FILE_VS) should work

Block quote
but anyway dataPath(CONFIG_FILE_VS) should work

No, it doesn’t " File not found"
Omitting dataPath (…) is the cure.
Thanks
hk

@Ive0200 ===
your file is probably not in the data folder…

That’s what I wrote in my initial post
hk

@lve0200 ===
weird; i have answered without testing but i am quite sure… i ll test!

@lve0200 ====

so i tested and it works

  • myfile.txt is in the data folder, nothing more
void setup(){
 size(800,600); 
 String[] lines = loadStrings(dataPath("myFile.txt"));
 println (lines[1]);
///this println my first item without error
 
}