We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm upgrading an android game i've built some months ago ([https://play.google.com/store/apps/details?id=com.Sarothlon.MazeGenerator&hl=it](See it here)) so I first tested the new version on java mode and than in android mode where I'm not able to write textFiles i used to save some informations about the scores and some settings. The problem is that in android mode the PrintWriter class doesn't work. I think this is caused by the path of the argument of createWriter("data/textfile.txt"); I red that in android mode the path of the file should be absolute and not relative, but i couldn't find the textfile.txt searching in my smart phone. In addiction if an absolute path is requested it's still a problem because I think the path can change according to the different users'preferences (for example sd card or internal storage).
How can i fix this problem?
Answers
For example this doesn't work:
`//the file example.txt is saved in the data directory of the sketch //and in the file there is only the string "Hello World"
PrintWriter output;
void setup(){ fullScreen();
}`
In java mode the output would be
Hello World 123456789
But in android mode the output is
Hello World Hello World
Hopefully any of the following post can be helpful in your case:
https://forum.processing.org/two/discussion/21167/accessing-sd-cards-for-storage#latest
https://forum.processing.org/two/discussion/9893/save-images-in-specific-folders-android#latest
https://forum.processing.org/two/discussion/21424/using-android-studio-how-to-i-load-media-files#latest
Kf
@kfrajer I'm trying this https://developer.android.com/guide/topics/data/data-storage.html#filesInternal with this sketch:
but processing doesn't recognise the openFileOutput() function and gives me this error
https://stackoverflow.com/questions/3625837/android-what-is-wrong-with-openfileoutput
So you can check this post: https://forum.processing.org/two/discussion/comment/100539/#Comment_100539
and then
context.openFileOutput(...)
Notice this is untested.Kf
@kfrajer it still doesn't work
I am not familiar with openFileOutput() and I can only advise based on my experience as documented in previous posts. I am not able to test your code as I do not have an Android environment available but until later in the day. Keep in mind Android Mode is having a revamp and some examples that ran in previous mode (Android Mode 3.0.2 and before) might not work with the latest Android mode (Version 4.0 beta ?). To make them work, they might need some minor tweaking. I will try later and I let you know. Just for curiosity, did you try my first link where they used other functions to do what you want to do?
By the way, can you comment if you have set the proper sketch permissions for writing to external storage? This is not related to your error btw...
Kf
@kfrajer I've found out a way to solve this problem.
1) Sketch permissions : •READ_EXTERNAL_STORAGE •WRITE_EXTERNAL_STORAGE
2)The files you wonna rewrite MUST NOT be in the data folder of the sketch, they MUST NOT exist when you load the program to the smartphone, or the function I will show you later won't have any effect on the files(I don't know why).The strange thing is that if you call my function, a new file will be created in the data folder and it will be accessible from the loadString() function.But if you create the file manually, before to load the program, loadString() function will be able to read only the original file, not any modification applied to it by my function.
3)Imports:
4)I wrote a function to write any file
For more detailed informations see this page https://developer.android.com/training/basics/data-storage/files.html#GetWritePermission
@sarrio===
if you want to be sure add some code like this one:
So it is absolutely normal that your method has no effects upon the file manually created in your data folder, even if they have the same name...
Great you got it working.
For the file access, The PRIVATE parameter could be the culprit. Check this post: https://stackoverflow.com/questions/3625837/android-what-is-wrong-with-openfileoutput
Also consider this: https://developer.android.com/reference/android/content/Context.html
This further to consider from the previous link:
Kf