Android 14 - Processing 4.3 - Set for Android mode.
The following code sent to tablet Samsung S6 Lite. Reads into the string array but when appended will not write it back.
I am expecting to write to the sketch’s own space.
I have tried adding sketchPath and dataPath to no effect.
I have set and unset the Read/Write to EXTERNAL_STORAGE to no effect.
It does not give any errors, just my printout shown on the listing comments.
Any Help please.
EDIT: The savedGames.txt is originally in the data folder.
import java.io.File;
String[] savedGames;
void setup(){
size(585,585); //785
savedGames = loadStrings("savedGames.txt");
println(savedGames.length); //This prints 12 for strings in file & array
savedGames=append(savedGames,"test");
println(savedGames.length); //This prints 13 for strings in array
saveStrings("savedGames.txt", savedGames);
savedGames = loadStrings("savedGames.txt");
println(savedGames.length); //This prints 12 for strings in file
}
void draw(){
}
In general this is not possible on android, as the data you’ve rolled out to the device, resp. the folder where it is placed is read only. Unfortunately, the lightweight methods like saveStrings do not cause errors to see that.
You have several options, see two of it…
Load the data (ie with loadStrings) and put it into SharedPreferences see.
@mnse Thank you for your advice. I read what you pointed to with some difficulty due to small snippets of code which I have interpreted as below. I produced the following amendment and have ‘commented’ the result.
I have not understood where that location is but it does not matter as long as I can read and write files and they remain there until I delete them or I uninstall the sketch.
I have not understood if dirName now exists.
I do not see how to save my file at the location if it does exist.
Can you help with the last step please, creating my data file, saving it etc.
My attempt produced the error “Read only file system”.
import java.io.File;
import android.os.Environment;
//Required data storage folder, will exist or be created in external emulated SDCard
String state = Environment.getExternalStorageState();
String dirName = Environment.getExternalStorageDirectory().getAbsolutePath()+"/DataX";
String[] savedGames;
void setup(){
size(585,585); //785
println(state); //This prints "mounted"
println(dirName); //This prints "/storage/emulated/0/DataX"
savedGames = loadStrings("savedGames.txt");
println(savedGames.length); //This prints 12 for strings in file & array
savedGames=append(savedGames,"test");
println(savedGames.length); //This prints 13 for strings in array
File f=new File("savedGamesA.txt");
saveStrings(f, savedGames);
savedGames = loadStrings(f);
println(savedGames.length); //This prints 12 for strings in file
}
void draw(){
}
not having much time but guess this is enough for you as a starting point…
import android.os.Environment;
import android.content.Context;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.util.ArrayList;
void setup() {
size(585, 585); // 785
String infilename = "inSavedGames.txt"; // file you roll out with deployment
String outfilename = "outSavedGames.txt"; // file which can be modified on device
// Load strings from file
String[] savedGames = loadStrings(infilename);
if (savedGames == null) {
println("Error: Input file not found");
return;
}
// show content of input file
println("----------------------------");
println("content of source file :" + infilename);
for (String s : savedGames) {
println(s);
}
println("Number of lines in input file: " + savedGames.length); // This prints 4 for strings in file & array
println("----------------------------");
// write content of input file to device
String absolutePath = getActivity().getFileStreamPath(outfilename).getAbsolutePath();
try (PrintWriter p = new PrintWriter(getActivity().openFileOutput(outfilename, Context.MODE_PRIVATE))) {
for (String s : savedGames) {
println(String.format("writing (%s) to file: %s", s, absolutePath));
p.println(s);
}
}
catch (Exception e) {
println("Error writing: " + e);
}
println("----------------------------");
// read content of file on device
ArrayList<String> content = new ArrayList<>();
try (FileInputStream fis = getActivity().openFileInput(outfilename);
InputStreamReader inputStreamReader = new InputStreamReader(fis);
BufferedReader reader = new BufferedReader(inputStreamReader)) {
String line = reader.readLine();
while (line != null) {
content.add(line);
println(String.format("reading (%s) from file: %s", line, absolutePath));
line = reader.readLine();
}
}
catch (Exception e) {
println("Error reading: " + e);
}
println("----------------------------");
// show content after reading on device
println("Number of lines from output file: " + content.size()); // This prints 4 for strings in file & array
println("content after reading:");
for (String s : content) {
println(s);
}
}
void draw() {
}
console output:
----------------------------
content of source file :inSavedGames.txt
Line1
Line2
Line3
Line4
Number of lines in input file: 4
----------------------------
writing (Line1) to file: /data/user/0/processing.test.androidfile/files/outSavedGames.txt
writing (Line2) to file: /data/user/0/processing.test.androidfile/files/outSavedGames.txt
writing (Line3) to file: /data/user/0/processing.test.androidfile/files/outSavedGames.txt
writing (Line4) to file: /data/user/0/processing.test.androidfile/files/outSavedGames.txt
----------------------------
reading (Line1) from file: /data/user/0/processing.test.androidfile/files/outSavedGames.txt
reading (Line2) from file: /data/user/0/processing.test.androidfile/files/outSavedGames.txt
reading (Line3) from file: /data/user/0/processing.test.androidfile/files/outSavedGames.txt
reading (Line4) from file: /data/user/0/processing.test.androidfile/files/outSavedGames.txt
----------------------------
Number of lines from output file: 4
content after reading:
Line1
Line2
Line3
Line4
Hello again mnse. I cannot thank you enough after such a long time of attempting to solve this.
It works perfectly and I must admit, I would never have got there without your kind assistance.
At last I can convert my original game.
Thank you for your valuable time.
Cheers
enonod