Android Mode saving and loading txt file


#1

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.


#2

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


#3

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.


#4

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”


#5

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.


#6

#7

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.


#8

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