We are about to switch to a new forum software. Until then we have removed the registration on this forum.
In the java mode the code work when I use Android mode it don't work anymore,it only crash the app how can I make it work that.
JSONObject data;
int score=2;
int record;
void setup() { data = loadJSONObject("data.json");}
void draw () {
record = data.getInt("record");
record=score;
data.setInt("record", record);
saveJSONObject(data, "data/data.json");}
Answers
Are you trying to append data or to overwrite the data in your original file?
Do you have enabled permissions to write on your phone? Check the manifest file, hit ctrl+k and open manifest.xml to check for what permissions you have so far.
Kf
What permission do I have to search for?
This is my file and my AndroidManifest
In the Processing IDE in android mode, go to menu Android>>sketch permissions and check writing_external_storage. What this does is to add a couple of tags in your manifest file related to permissions.
For more info check https://developer.android.com/guide/topics/security/permissions.html
Kf
It doesn't change nothing it still crash and in theory i'm not accessing to any external storage
The app in theory should have access to his local file
Ok, that didn't work. My next step will be either:
This is not a final solution but more to understand what is happening to your program.
Kf
Also here there is a problem with save string
If I remove saveStrings the app don't crash
@MrAlko99=== i think that it crashes because you use a file separator in the filename, this works with java but not for android.
yeah but then how can I read the file from the main folder?
@MrAlko99===
As for reading you can use the standard ways=
String myString = json.getString("some string")...
writing is more difficult because Android does not allow to modify the values stored in the ApplicationContext (assets or res) at runtime
So if you want read/write you have
1) to put your initial .json in your data folder
2) to copy it by code on sdcard
3) then you can access to it with fileIO and modify if needed
this is easy with a text file but i never try with jSon; i ll give a look.
Yeah but I want that the code can modify the .json file
@MrAlko99===
i have tried and it works.
Thank you very much when I will go back home I will fix it, how it would be the directory for a file in the external storage?
@MrAlko99=== depends; so you have to ask for that (in your setup, try!)
of course you have to import::: import android.os.Environment;
@MrAlkoo99===
using this way and json will work; yet it is perhaps too much if you want only to save 1 or 3 values (string, int, arrays) - as for a score in a game; in this very basic case i dont think that json is mandatory; as for me i would use SharedPreferences from android.
How you done that, and how do I import libraries??
Initial not working code
P.S. This code actually work for the AbsolutePath
I believe you got the error when you tried to run it. Maybe try to put it in context. To do that, search for an existing example in the forum:
https://forum.processing.org/two/search?Search=getExternalStorageDirectory
After you find an example, configure it to your case and run it. The way you run your code should not work in Android mode (I think... I haven't checked it myself as I don't dig under the hood of the Android mode engine).
Also, no screenshots of your problems. Copy and paste your code and your error messages. This will allow to: 1. Run your application by other forum goers and reproduce issues 2. It will make it legible (some screenshots are bad) 3. People can search for your error in this forum
Kf
How can I check if there is a file in a directory
exists()??
https://forum.processing.org/two/discussion/comment/77672/#Comment_77672
Kf
SOLVED
Thank you very much for all your help @kfrajer @akenaton