I have two XML files located in res/xml/. One file is a normal XML file located in that directory called myfile.xml and I can access it normally as R.xml.myfile.
Eclipse allows you to link files in from other locations. I have another XML file that is linked in from another drive. No matter what I do, i can not access this file by R.xml.newfile. I've even tried a DTD file, and isn't available via R either.
Am I doing something wrong, or is this some kind of bug?
No matter what I do, i can not access
this file by R.xml.newfile. I've even
tried a DTD file, and isn't available
via R either.
If the "linked" file is not in your res/ directory tree, aapt will not find it. If you can get Eclipse to set up "linked" files as symlinks in Linux/OS X, it might work. Or, you can skip the Eclipse "linked" concept and set up the symlink yourself. Or, you can create your own build script to copy the file from its existing spot to your project's res/ directory.
Well if the file is not in res or assest folders or inside your project how would it be available when the application is going to execute on actual device.
If the requirements remain same try accessing the xml file generated by another program as a network resource
use SAX Parser to regain the XML data, i dont know about DTD, but i prefer to use SAX or DOM to access a XML file. Sorry cant paste the code here since the SAX uses lot of classes, so tell me whether you need it or not
Related
Lets say I want to store a JavaScript file in my app for an Android WebView to use. Where should I put this file? My first though would be somewhere in the assets folder, but I am not too sure.
Check the following link and explanations:
http://www.41post.com/3985/programming/android-loading-files-from-the-assets-and-raw-folders
The Assets folder is an 'appendix' directory. The R class does not
generate IDs for the files placed there, so its less compatible with
some Android classes and methods. Also, it’s much slower to access a
file inside it, since you will need to get a handle to it based on a
String. There is also a 1MB size limit for files placed inside the
Assets folder, however some operations are more easily done by placing
files in this folder, like copying a database file to the system’s
memory. There’s no (easy) way to create an Android XML reference to
files inside the Assets folder.
There is also a raw folder you can even use that, but:
it’s important to highlight the main differences between the raw
folder and the Assets folder. Since raw is a subfolder of Resources
(res), Android will automatically generate an ID for any file located
inside it. This ID is then stored an the R class that will act as a
reference to a file, meaning it can be easily accessed from other
Android classes and methods and even in Android XML files.
Using the
automatically generated ID is the fastest way to have access to a file
in Android.
For completing the answer, you can easily use file:///android_asset/
Have a look at this question:
Android WebView Javascript from assets
I have a short question about writing to a file in android. I am writing a game where I use a xml file to save some data about the level stats. Now I have seen that if I save this xml file in AssetManager it is not possible to change it (only permissions to read files).
Now because I can only modify files which are in the filesystem of android (using openFileInput and openFileOutput to work with it) I wonder where I have to save my (already existing) xml file in my eclipse project so that I can use openFileInput to load it and change it via code.
Do I have to make a new folder? E.g. project_path/files/myxml.xml.
Is it even possible to load a file which was created (outside the AssetManager folder) before installing the .apk to target?
If it is possible does anybody have some example code?
I hope you understand my question.
There is no such place. Installation of android apps does not include an automatic step that would copy your content from apk to the internal folder (and your application does not reside in the folder either).
You will have to create your XML file in code, possibly checking for its existence before each access (or using some other marker).
I am trying to design a new Android application.
For that application I will have an XML file that will be located somewhere on the server. This file will be generated from the mySQL DB.
For now (developmental phase) I got a simple and small XML file that I need to put in the Android Eclipse project in order to read it and present the data on the phone.
I just tried to put this file in res/values, but compiler gives me an error: "Invalid start tag".
Looking through the stackoverflow and google I see a lot of different answers and google even give me an answer of how to parse xml file on Android. ;-)
So is there a "standard" place where such XML file goes in Android Eclipse project? Think about it as the data that is read from the DB.
Some answers are to place it in the res/xml folder. I just made a brand new Android project and I don't see such folder in it. Do I create one? Shouldn't it be done automatically?
Some says you need to put it the res/raw folder. Again it is not present in the Eclipse project. Do I make one? Shouldn't it be present already?
Please clarify.
Shouldn't it be present already?
It doesn't have to. There can be thousands of folders inside res/ folder. You don't want to have them all at first.
Do I make one?
Yes. Add folders when you need them.
Some answers are to place it in the res/xml folder.
Some says you need to put it the res/raw folder.
The difference between these folders is that files inside raw stay the same you put them and inside xml are parsed when APK is created and put there in a binary, optimized form, similar to what happens to layouts, AndroidManifest and other.
I'm very new to android. I would like to create a package that upon install would put files in the app folder (/data/data/my.app/...) of the android file system.
I would like to do this, so I could access the files using standard java File methods (in order to reuse my own code), and not have to use the android resource accessing methods.
The only way I thought might achieve something in this direction, is to put the files in the /res folder, and copy them to the file system in runtime (on first run, for example). This has the disadvantage of having two copies of each file for no reason.
Another way could be to get my app to download these files from a server (saw this option in another answer as well). This is possible, but I would wish to avoid having to put up a file server for such a simple task...
To my understanding, DDMS will not help here, as it only allows me to access the folder manually, after the application is installed.
My question is: Is there a different way to achieve my original goal (accessing files using standard java methods)? If not, is there a cleaner way to put files on the file system?
Thanks!
There are 3 methods I can think of that avoid duplication:
As you mentioned using the res/raw folder. You can avoid duplication if your existing code uses can use InputStream instead of File and use Resources.openRawResource().
Use the assets folder and access the file using file using AssetManager.openNotAssetFd(). Again this would require the use of Streams not File class. This can get a bit messy if your file is compressed because assets are memory mapped.
If you code is really tied to File (doesn't just use it to open an InputStream). To avoid duplication you could download the file from the web on the first time it is run and store it into the external storage.
You can put your files on under /res/raw and then use:
Resources res = context.getResources();
InputStream in_s = res.openRawResource(R.raw.myfile);
Or if you want the File object
File f = new File(context.getResources().openRawResource(R.raw.myfile));
Then you can use normal Java apis to work with your files.
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!