I'm trying to find the best way to store XML files from the internet, so that i can parse them at a later date. I have no problems with downloading an xml file, and storing it in the
/data/data/package_name/files
folder. However i'm not sure how to read this file after it has been saved there, and to pass that read file to an instance of a SAX Parser. Additionally, i have noticed that it is quite slow to read the file from the phone, which is a concern.
I have read the storing XML files in the res/xml folder is a lot quicker - but i'm not sure if it's possible to save a file from the internet into this folder at runtime?
Any help would be appreciated :)
Cheeers
Store it in the cache directory.
getCacheDir();
so you can say filename = getCacheDir() + file123.xml;
You cant write to the res folder at all. These resources are compiled into your app at build time and are read only. As for reading the file, You should just be able to create an input stream for it in the standard java way and parse out the bytes using your sax parser.
This section of the guide describes how to access the internal and external filesystems and how to get input/output streams.
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
Related
I have code for creating an internal file, there is random algorithem that create the data stored in it and i want any app to have the same file with the same binary data in it.
so i need to make the file on my desktop and add it to internal files some how.
my question is what do you think is the best way to do it.
i thought to locate it in my project, read it, and write it to internal files.
the problem is, i dont know where to locate my file in android studio so that it will be included in the external files and then where to read it from.
thanks. =]
hope i made myself clear.
Put it in src/main/assets/.
You can then access your file with AssetManager and do whatever you want with it.
From the Android Developers website:
main/assets/
This is empty. You can use it to store raw asset files. Files that you
save here are compiled into an .apk file as-is, and the original
filename is preserved. You can navigate this directory in the same way
as a typical file system using URIs and read files as a stream of
bytes using the AssetManager. For example, this is a good location for
textures and game data.
You need to move that into the assets folder. From there you can refer to the file.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
where to place an xml file with data in app
I'm kinda new to android dev. I'm storing the data related to my app in an XML file. I'm trying to use SAX parser to read/write/edit data to that xml file.
Where is the proper place to store this xml file? In assets/res/some-other folder ? (I want to use internal memory)
Is there any problem with saving app data to internal memory? This app won't have huge amounts of data. I need to read/ write/ modify this xml file depending on user actions. (I read that we can't edit files in assests at runtime !!)
Given a particular directory, how can I access it using InputStream in order to feed it to SAX XMLReader?
If you want also to write the file, I don't think that you can do that in /res or /assets. Take a look at the android data storage options. For example, you can write the XML file to the internal storage.
http://developer.android.com/guide/topics/data/data-storage.html
Yes, you can store your xml files in assets.
But I suggest you to create a folder name XML in /res/. It'll help you to easily use it with id "R.xml.xml_file".
Though it depends on entirely your requirements, I would suggest better to create a directory using getDir(String dirName, int mode) and store your xmls here.
Above can be used to store the data dynamically means when application is running. But if your application is already having xml files, its better to store it in asset folder because you can create sub directories easily here.
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.
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().
I am trying to load an xml file located in the /assets folder of an android project by name using this method:
getAssets().openXmlResourceParser("thefilename.xml");
However, executing this code always throws a "FileNotFound" exception, even though the file is located in the /assets folder and is with the correct file name.
Now, I have not put the file in the /res/xml folder because I really need to be able to 1. edit the file right on the device itself and most importantly 2. add new xml files to the application without issuing an update, to allow for easy user modifications.
Thanks in advance.
I think what you are looking for is either getAssets().open("thefilename.xml") or getAssets().openFd("thefilename.xml") depending on what the end use of the file is. You can see from Dianne's response in this post awhile back that openXmlResourceParser() is not really usable just to gain access to files in the assets/ directory: http://goo.gl/2KfgT
From there you will have a stream that you could feed into a SAXParser or do whatever else you choose.
Side Note: On the points you mentioned you really can't edit files directly in assets/ or add new files to assets/ at runtime. You will need to work with files on either internal or external storage to do those things. You probably already knew that, but I thought I'd mention it.
Hope that Helps!