So it’s all in the title. I have no idea what to do. I was hoping loadStrings and saveStrings would work but it didn’t so I’m assuming these don’t work with processing for android. So how would I go about tracking a high score? All I need is to track a single number.
Check this How to save score - Processing 2.x and 3.x Forum to explore a relevant post related to storing scores.
Can you describe your experience? What did you try and what errors did you get, if any?
I believe this next should work for IO operations:
final String FNAME="readWriteFile.txt"; //For reading Strings lines=loadStrings(sketchPath(FNAME)); //For writting saveStrings(sketchPath(FNAME),lines);
I have made a scoreboard using a text document before, and I don’t know what I was doing but it wasn’t working before. The only change I made was that I had loadStrings(“filename.txt”) and changed it to using a final string but I don’t think that should make a difference. Maybe I had a typo or something and was pessimistic towards text files working with android for some reason.
So anyways it’s working fine now. Thanks
Ok so it’s been working a while, but I was testing stuff with getting a new high score so I deleted the app on my phone to remove the high score and then ran it again, and now it is not working. I have the txt file in the data folder. It is giving me the error that the text file contains a path separator.
Please try accessing your file using
sketchPath("filename.txt") instead. From what I understand, when you try to access data from your data folder, this data is actually coming from your assets, and Android API dictates that assets are read only.
Feel free exploring this guide: https://developer.android.com/guide/topics/data/data-storage
It provides you some ideas of options available to you when it comes to data persistence solutions. In general,
saveStrings() should work. I will be doing some testing but I am like 99.9% sure my solution would work for you. Please tell us if it works for you. What Android APi are you working on, and are you using the latest Android mode version in Processing? There is a second solution documented in the previous forum (let me know if you can’t find it) about storing data in an external storage, outside your app internal allocated resources by Android OS. The disadvantage of this option is that requires permission to write to external storage and you need to check the external storage is available (or if the device has one). More work, but it works as well.
Here I quote from the link I provided you:
By default, files saved to the internal storage are private to your app, and other apps cannot access them (nor can the user, unless they have root access). This makes internal storage a good place for internal app data that the user doesn’t need to directly access. The system provides a private directory on the file system for each app where you can organize any files your app needs.
When the user uninstalls your app, the files saved on the internal storage are removed. Because of this behavior, you should not use internal storage to save anything the user expects to persist independenly of your app. For example, if your app allows users to capture photos, the user would expect that they can access those photos even after they uninstall your app. So you should instead save those types of files to the public external storage.
To learn more, read how to save a file on internal storage.
Internal cache files
If you’d like to keep some data temporarily, rather than store it persistently, you should use the special cache directory to save the data. Each app has a private cache directory specifically for these kinds of files. When the device is low on internal storage space, Android may delete these cache files to recover space. However, you should not rely on the system to clean up these files for you. You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB. When the user uninstalls your app, these files are removed.
For more information, see how to write a cache file.
Lastly, a third solution can be done based on https://developer.android.com/training/data-storage/shared-preferences
Ok, so I was doing some testing and these are my findings:
//This works if and only if file already exists (created by your app at some previous ocassion) String iofile = dataPath("dataDataLevel2.txt"); String lines=loadStrings(iofile);
//This works if and only if file already exists (created by your app at some previous ocassion) String iofile = sketchPath("dataDataLevel2.txt"); String lines=loadStrings(iofile);
//This DOESN'T works String iofile = ("dataDataLevel2.txt"); String lines=loadStrings(iofile);
Notice, this operations will only success if the file exists. It didn’t assume anything about any files created manually in the PDE before running the app. So the proper approach is that before you load the file, you check that the file exists. If it doesn’t exist, then you need to create it.
A quick extra note.
Now, let’s assume you have manually created a file inside your data folder called “file6”. I have news. If you do this:
//This works String iofile = ("file6"); String lines=loadStrings(iofile);
Why? This is part of the design by the Proecessing Foundation. When you build your app, the files in your
data folder are moved into the
assets folder (you can see this folder when you exported your app… do this as an exercise). Then the above snippet will access “file6”. However, if you do the following:
saveStrings(iofile,lines);, this will work BUT with a caveat. The file is not saved in the
assets folder. The file is saved at other location, a private location dedicated to your app. All apps gets a private location assigned when they run in a device. All apps can access this private location. In processing, to access this private location, you need to use
So in a nutshell: File located in the
data folder when you build your app from the PDE, they are moved to the
assets and they are meant to be READ ONLY.
You can get items from
assets manually using the following code:
surface.getAssets().open("file6"). You can see more here: AssetManager. Check the overloaded entries for
open(). The accesMode are constants defined in the prev link. For completion to this post, one can access assets using
Additionally: Files created in the private space assigned by Android (accessible by using
sketchPath("file6"); is private meaning you won’t be able to access this file using your device’s favorite file browser. I guess if you rooted your phone then you should be able to see it. Nevertheless, you can access these files using
sketchPath("") as prev described.
If you want to be able to access the file using an app file browser in your device, then you need to write to the external storage. I might write the code how to do so later, or you can explore previous posts:
Other storage alternatives: Using a database.
Finally, as described in the Android API for data persistence and storage, there are other options for small data sets and for temporal data storage. For instance: How to save score - Processing 2.x and 3.x Forum, a link a share earlier in this post.
Thanks a ton for the help. This really solved it for me. It is working correctly right now, and after deleting the app and re-doing it a couple times, it looks like this will work for sure. Thanks a ton for the help.