What is best practice for shipping editable XML with your application? - android

Hi fellow android developers!
I am developing an application where I have XML files that contain my data. When doing edits in these data, I save the data to the XML files, thus these must be editable.
This I would be able to achieve using the local storage for my application with the openFileOutput method of my Context.
But how would I go around shipping my program with these datafiles already there, with some pre-filled data?
I can see the option of shipping with some XML files in my res/xml or res/raw, duplicate them to the local data storage, but then I would be unable to remove the files in my resources, and this would take up too much storage.
Please tell me what you would do in this case?

You can not include editable files with your application.
So you will have to write them to the local file system some way. Either by downloading them or including them as raw resources via openRawResource().

Related

Where to put files for External Storage Android

So I have some files I want my Android App to access, read and write.
I want to store it internally. Where can I put these files in my Java Project so they are accessible or can this not be done?
There are three ways to achieve this, and according to your requirements select the approch
on SDCARD
This is the normal SDCARD/in-build SDCARD in newer smart phones. you need to create specific folder structure and put your files there, here you can do file read and write both
but this in insecure because accessible to all the application
on Internal Storage
This is given as Applicaiton specific storage where you can create the file and do the operation, this is most secure way to do it, but this is generated run time so you can not push the files directly, you can put your files in RAW or ASSETS and copy that here
RAW and ASSETS
This is in the code structure only and only read access is given to this folder, you can not change this file run time.
if you select any one of this approach then simple goggling will show you the sample code.
You can read or write files in the path of your internal storage as
data/data/package_name/files . I had already answered a similar question you can check it out.

Best way to organize editable internal data files for Android app?

I want to use data from an xml file to populate AutoCompleteTextViews and MultiAutoCompleteTextViews, and I also want to be able to edit this file while the app is running so that the AutoCompleteTextViews can be updated with new information. What is the best way to go about this? I know that files in the /res folder cannot be written to during runtime, and writing to external storage is slower than accessing internal storage.
One way you could do this is by creating your own SQLite database.
I would make sure that the scope of what you are doing merits a database, though.
The other option is just to create a flat file with keyValuePairs and look up the value with the view's ID as the key. This could be stored in external storage. Access speed should not be a big concern for a text file.

Is it possible to reference a strings.xml file that is not saved in your res folder?

I am creating an app that could potentially be used in multiple educational establishments across a variety of courses with tutors who will want to be able to update some of the information within the app themselves on an ad hoc basis. I originally thought that the best way to do this would be to have the application download a new strings.xml file to the res/values folder, though I have read that you cannot update this folder/file whilst the app is packaged and running. I think a good work around for this would be to be able to save another strings.xml file elsewhere
My questions are:
Is this at all possible?
Where would I go about saving the strings.xml so that it is not
packaged when I export the app?
note: The file will not be called string.xml so there will be no confusion etc. with the actual strings.xml.
There is no "rule" of where you should put the file (minus, of couse system and other private folders you can't access). However, the logical and most common place to put a non-packaged resource that your app downloads would either be in your own applications data folder (located on the internal storage of the device) or on the external storage of the device (SD card).
To write your file to the internal storage you will need to use the context's openFileOutput(..) method. This stores the file within your apps private data directory. Use openFileInput(..) to read your stored file
To write to external storage you will need to add the WRITE_EXTERNAL_STORAGE permission to your manifest. After doing that, you can use FileOutputStream to write your files data when downloading. InputStream for reading (look up which input stream type would be most suitable)
Obviously, the examples I'm giving aren't fully detailed or have code but they will guide you in the right direction for storing files on your device.
Files under the res folder are used by the compiler to autogenerate the R class that contains the ids of the strings,layouts,drawables etc...
Of course you can download a custom resource from your server and stored it in the SD as #dymmeh points. And is the most reasonable way of to achieve modification of literals but be aware, you will not be able to use the #string/string_id in your layout's xml and you will have to parse the downloaded file yourself.

Choosing the right data storage alternative

I'm coding an android app for parsing sms messages. I need different rules for different countries and idea is to define rules for my country and later open source project so other users can contribute with rules for their countries. XML format is the most convenient in my opinion for defining these rules.
So I have an app which reeds rules from XML file stored in res/raw and users will later be able to update that set of rules or download rules for some other country when those become available.
As I said I have put existing XML files in res/raw, but the problem is when users download new or changed set of rules application can not change or store files in resources (correct me if I'm wrong).
So I have two options now, and I don't like either one. I would like to hear your opinions on the matter.
a) I can keep XML files as local files, but then I must read in default rules from res/raw and copy them into file system on application installation, or keep track of two separate bunch of files. Is it possible to preinclude files on file system when installing an app?
b) I can keep XML code in database, but also copy it from files at install time. Or even worse code XML as String in class to insert it on install?
I apologize if question is too long or unclear, English is not my native language and this is my first question.
I'm not sure if i understood you right, but you could store the XML-file in the internal data storage of the application. --> http://developer.android.com/guide/topics/data/data-storage.html
If you want to attach a first Version of the XML-file on install, store it in the ressources first and copy it to the internal data storage after install. On updates of the XML file you can just overwrite this file in the internal data storage.
Hope i could help you, this is my first answer on stackoverflow. I'm sorry if i just told you things you already knew.

what kind of file should i use in android to reference information that i stored?

I have created an android app that calculates the numerical values of word, and gives you a list of other words with the same numerical value. The way I have been doing it, is storring the words and value in a .properties file. Ie. A line from a .proprties file called "myWords" will have something like: 61=you, then I just use a get() method to call it,
ie. String myString = ResourseBundle.get("myWords").get("61"); would return the string "you". Is there a better way to do this? My guess is that this is not the proper use of a .properties file, and I was wondering if there was another way to do this correctly. I want to include the file in assets folder of the app, and from my limited understanding of sqlite, you can create a file within android, but you can't just include a file in the assets folder, and then read it. So that said, is there some other type of file that I should use, or was I wrong about sqlite, or is the .properties file being used correctly?
SQLite is your best bet and is the best way to handle your data on an Android phone, that is why Google bundled it on Android in the first place, to avoid people the pain of dealing with files.
If you follow this Tutorial they will show you how to create your database in your computer and then load it up on your "assets" folder and access it from your Android application.
Hope that helps!
You can use a csv file, read it from the assets folder each time the app starts or only once after installation and then store the values in a database.
Take a look at my answer here on how to read the files included in your app (you would use a csv file instead of a libray, but it's still reading files): Hosting an executable within Android application
Edit: here's another example to read from the assets folder: Image uploaded from the android app space seems corrupted
You can try out database option. Here is an interesting tutorial on how to pre-populate a database and then ship it out in the APK.

Categories

Resources