I can read files from data directory. I want to allow user to copy files to the directory and the app reads them. My problem, when I build the app on Android phone I’ve no idea where the data directory is.
Alternatively, I’m trying to make my app to read from external /storage/emulated/0/ unfortunately I got AccessDeniedException (READ_EXTERNAL_STORAGE permission ticked). I’ve tried many suggested solutions for permission from internet without success.
Questions:
Can someone point me to some samples to read file from external storage?
Another solution, if I can find the data directory on the phone then I simply copy the files to there. Where is the directory?
This not my code I don’t remember where I got it I think it’s belong for @noel
I have other code you have to determined the folder of saved data inside sketch I am going to post it when I found it
For now see this code
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");
}
}
Thank you for all the information. I’ll go through it and see.
Currently, I’m still seeing loadStrings():
java.lang.IllegalArgumentException: File /storage/emulated/0/twinkBOSS/env.txt contains a path separator
which I had before. I also tried ‘//’ without success.
I also tried:
I added:
println( listFile[i]);
in the ‘else’ section and didn’t see any file listed at all. It basically goes through all directories recursively without listing any files. Probably, missing permission in accessing files. I’ve also found articles about permission changes in Android 10 and some older solutions won’t work any more. Can this be a reason?
But got “File not found” on the screen.
When I tried to load an image all went well. Permission to read external media should be okay. However it doesn’t want to read text file.
String[] lines;
int index = 0;
String filepath = "/storage/emulated/0/test/positions.txt/";
void setup() {
size(800, 800);
background(0);
stroke(255);
frameRate(12);
lines = loadStrings(filepath);
}
void draw() {
if (index < lines.length) {
String[] pieces = split(lines[index], '\t');
if (pieces.length == 2) {
int x = int(pieces[0]) * 2;
int y = int(pieces[1]) * 2;
point(x, y);
}
// Go to the next line for the next run through draw()
index = index + 1;
}
}
java.io.FileNotFoundException: /storage/emulated/0/twinkBOSS/out.txt: open failed: EPERM (Operation not permitted)
When I ran it with an existing file I got:
java.io.FileNotFoundException: /storage/emulated/0/twinkBOSS/env.txt: open failed: EACCES (Permission denied)
I was hoping if it can’t create file at least it can append to existing file.
Does this has something to do with my Processing IDE 3.5.4 and Android 11?
I tried to run Processing4. Unfortunately, the IDE itself is facing some errors.