Holding Information in Android App - java

I've only just started to write in Java on Android, so please bear with me.
I have some settings I want to hold in my app, normally I would have used an xml file. Trouble is i'm not sure how to load it into the xml parser to read it.
I thought I might be able to drop it into /res/values/Info.xml and open it from there but it does'nt find the file.
I have also read that people are starting to use a SQLite database to hold information in, is this more the standard way to go?
thanks a lot
Luke

It sounds like what you want are Shared Preferences. Its a simple way of storing key value pairs, along with a UI for letting the user change them.

You could try storing to External Files (that way they can save settings on an SD card if they have 2.2). You wouldn't be able to modify the files in the res folder like you tried because these files get compiled into the app package. You could also try Internal Files found on the same page. SQLite might be a bit much for config settings.

Related

How to access files under /sdcard/Download in Android 11 without SAF? [duplicate]

I'm still searching, but maybe anyone faced with the same issue.
I'm maintaining an app. This app stores some data in two places (lets say on sdcard/example and external_memory/example). Before the migration to the newest Android Api everything worked fine using ExternalStorage.getAllExternalStoragePaths(). Now I'm still able to get both paths, but the sdcard/example path is not readable and not writable..
I read about the SAF, but in my case that is not a solution - the files stored in both directories are parsed using a native library (C++) - I need path to it like /storage/sdcard/example/test.xml. Otherwise the library is not able to read this file. Unfortunately I cannot change the library because of missing sources.
I would like to ask three questions:
Is there any other way to ask access to directory on SD Card and use this directory like the old way?
Maybe I can say Android - "hey, I want have Example directory on SD Card and maintain it"?
Or maybe I can use SAF without ContentResolver?
Moving files to /storage/sdcard/Android/data/com.example/files/ is not acceptable by stakeholders.
Is there any other way to ask access to directory on SD Card and use this directory like the old way?
No, short of building a custom ROM or rooting the device.
Maybe I can say Android - "hey, I want have Example directory on SD Card and maintain it"?
No. The closest thing is getExternalFilesDirs(), getExternalCacheDirs(), and getExternalMediaDirs(). If those return 2+ items, the second and subsequent ones will be on removable media, and you can read and write to those paths. However, you do not control what the paths are.
Or maybe I can use SAF without ContentResolver?
No, insofar as you cannot get a filesystem path.

How to load and save files in Android?

I need to load "configuration" type files for my program in Android, they are both .bin files containing dictionary data for the NLP library. I'm a bit new to Android still, and I'm having trouble finding a folder to place the files in so I can access them when the activity starts.
I also need to create/save/load a filetype specific to my program, and I don't know where to put it either. All I've been able to find on here is people using the getAssetManager() function to fetch input streams, but I explicitly need File objects for me to be able to load them into my pre-existing desktop software code I'd like to reuse (plus the libraries require them anyway)
I've also seen people using a "res/raw" folder, however the ADT did not generate this "raw" file when I made the project - so I'm not sure what to do there either.
Here is how I usually start the software in the desktop version, but I need to fetch these files in an Android environment:
brain.start(new File("memboric.core"), new File("en_pos_maxent.bin"), new File("en_sent.bin"));
core = brain.getInterpreter().getCore();
The memboric.core file can be generated, but I need to know WHERE and HOW to do so.
Thank you very much for your time, feel free to direct me to other resources if you feel this question is inadequate.
TLDR; how do I load "static" files for the software to function (needs to be included with software), and how to create/load/save "personal" files into an appropriate area of the device?
Use Context.getFilesDir(). Your application can read and write files in that folder and they'll automatically get deleted if your application gets uninstalled.
From that point forward, you can create, delete and read from files like any other Java application.
the "raw"-folder you can create it on your own. So check this out, which shows how to handle files in Android: http://developer.android.com/training/basics/data-storage/files.html

Saves game level settings

I have been coding for about a month and I have found ways to adapt around ever problem but one. The problem as you can probably see by the title is how to make a way to make game saves. I am currently creating a very simple game that has about 5 classes of my code and maybe 2 of Java Swing GUI.
I know how I would like to go about the saving process but I have no idea how to do it in my code. How I would like to go about doing this is by making the code print a Number or Integer to a file to represent a Level. For example if you completed level 1 the number in the file would be 1. I have tried some templates for this but none of them work.
I understand how to write to a file but my problem is reading it from a jar or even creating a file then reading it from a place on the computer. I need to know how to find a file URL for different computers because some use Docs and Settings and other Users. Please could someone help.
Since the jar is read only, it can only contain the 'default settings'. See this answer for the general strategy to deal with such a embedded-resource.
Speaking of which (embedded resources) see the info. page for more details on how to access them.
Here is an example of storing and reading a Properties file from the 'current directory'.
As mentioned by #MadProgrammer though, it is safest to put the settings file into a (sub-directory) of user.home, as seen in this answer.
But a properties file is just one option. You might also serialize an object, or write the file in a custom format that your app. knows how to read, for the first two off the top of my head.
Besides 'serialize (in some form) in a File', there is also the Preferences API, or for desktop applications launched using Java Web Start, the PersistenceService. Here is a demo. of the service.
I need to know how to find a file url for different computers because
some use Docs and Settings and other Users
The System property user.home points to the user's home directory
File userHome = new File(System.getProperty("user.home"));

Data storage within jar

I am trying to create a java standalone program and was wondering if there is a way to have some sort of data storage within my jar?
You see, my initial idea was to use xml files within the jar for reading and writing stuff in. I had no problem reading stuff from the xml files but I was told you cant really edit/create new files within a jar.
I want it so when i send the executable jar to my friend, initial data I have put in will be in it already and then they can just add to or change the data. I find it pretty tricky as it has to be a form of data storage without the internet access or any need for my friend to install anything more.
I decided to go for sqlite in the end. It works exactly how i want it to work. I might look into javaDB in the future as it seems to have better SQL language support sqlite.org/cvstrac/wiki?p=SqliteVersusDerby
As far as I know, JAR files are read-only, which cannot be rewritten to.
My suggestion is to use MS Access database as your JAR's data keeper.
As it is portable, the only weakness is that you have to bring both of your JAR and MDB files together (or put them in 1 folder).

How to store high score inside a jar file

I am developing a small game in Java and I am shipping it as a single Jar file. I want to store the high scores/best times for that game somewhere. Instead of storing it in a separate file, I would like to store it in the application itself (inside the Jar) so that its not lost. Is this possible at all ? If so, how to do it programatically.
Java does not give you tools to modify the JARs which are currently run. If you really want to do it, you have to guess the location of the JAR by yourself (which might reside on a read-only filesystem) and modify it the same way you would modify any archive file.
Bottom line: it's a very bad idea, don't do it! See this question for a much more reasonable solution.
Nothing is impossible, but storing it in the jar file would make it very complicated. You might also end up with unwanted side effects like "Permission Denied" errors when the jar is owned by another user. Virus scanners might get nervous when they see jar files change without reason, etc....
I would look to the Preferences API for storing this kind of info.
I think it is a bad idea to try and store anything in the jar file. Another option is to have a web based service offered to the people playing with your game. The game could connect through a web service to your hosted server and then store everything centrally there. Not sure if it is exactly what you want but it's just an idea. It would also allow people to compete with each other.
Java JAR file is a ZIP-Archive, so you could possibly access it with standard ZIP-Tools and just extract one hisghscores.txt file, modify it and then pack it back again.

Categories

Resources