Android RandomAccessFile from Assets directory - android

I would like to port some code to Android(more pecisely an XML parser net.xqhs.util.XML ) in order to do that I need to read files from assets or raw folder using RandomAccessFile.
How can I do this?
Thank you!

That is not possible, sorry. Neither an asset nor a resource is a file on the Android device, and so while you can get an InputStream on them, you cannot create a RandomAccessFile on them.
Your choices are:
Use any of the available XML parsers for Android that take an InputStream. There are three XML parsers that are part of Android (DOM, SAX, XmlPullParser) that can work with streams. I would expect most of the third-party XML parsers to also support InputStream.
Copy the asset or raw resource (e.g., using an InputStream) to a local file. Then, create a RandomAccessFile for that file.

Related

RandomAccessFile from asset,raw,etc. folder file

I want to use RandomAccessFile in my app and one of the requirenment is to keep my files inside app. So I'm able to read files from resouces, but in case of using RandomAccessFile I need String or File instance to work with it.
And As I know Is not possible to get absolute path of asset file or of file in raw folder. Maybe some workaround solution is here? How to read resource file using RandomAccessFile?

How to access a resource as "File"

I have a few files as resources in the android app.
On the other hand I have some methods that require a "File" object as an argument.
How can I bridge the two?
Edit: I need some way to have a "File" object and not to read the file as an inputstream. thank you!
You could copy the file to external storage (reading from your raw resource's InputStream and writing to a new file) and then open it as a File from there. That's kludgy, but I'm pretty sure there's no way to get the resource as a File directly.

Get a xml file from sdcard to android app?

I need to save a xml file from sdcard to my android app, in res/values. How can I do this? Can anyone help me?
just use the java file command for your xml file on sdcard.
File file=new File("/mnt/sdcard/example.xml");
and using DOM or SAX parser you can parse the data(information) from it for your application.
Thnx.
EDIT: parsing local xml file using Sax in android, Read XML Resources in Android, using XmlResourceParser: XML parsing interface or Here or Here Look at these tutorials.
They are just folders in an Android project when you develop. When the app is compiled, everything in src and res is turned into binary code, and packed in one apk. Therefore I don't think that you will have res folder or anything like that in the device.

Using XmlResourceParser to parse xml file in internal storage

I have an application where at one stage, I need to download the latest xml file from a server and store it in internal storage (data/data/packagename/filename.xml). I've searched online and its not possible to save file at runtime to res folder, so I have to use internal storage.
However, I'm struggling to try and parse the xml file. Previously I've been using the XmlResourceParser to parse static xml already stored in res/xml/ folder. It's very easy to use. So I'm wondering if theres a way to use XmlResourceParser to parse the newly downloaded xml file. Otherwise, how should I parse the xml file?
Thanks.
XmlResourceParser is for binary xml files in res folder only. But you can use XmlPullParser. It can read data from an InputStream.

Store and Parse XML from the internet

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

Categories

Resources