I have an Game App, and i'm using a lot of XML's I was wondering if those XML's are carried by the APK? will those XML files will be installed together with the application itself? Because there is a part of my App where I use DOM Parser to update a XML file and the updated XML file will have its content permanently unless you update it again
Any XML file in the res or asset folder will be shipped with your application inside the APK (be careful though as the XML files in the res folder might get compressed, so be sure to open them using context.getResources().getXml(R.xml.identifier);).
As for the second part of your question, any file packed in the APK cannot be modified at runtime. But you can copy the XML files from your app into a folder, either public on the SDCard or in your apps private directory (for example context.getDir("mydir", Context.MODE_PRIVATE); ).
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 was going through the docs of APK EXPANSION FILES in android and I was wondering that can we have all layout files in the patch apk and application's business logic in main apk ?
I read this Android Apps Break the 50MB Barrier too.
I tried creating this but the problem I faced was our widget names need to be in R.java file which is generated automatically..If I have layout files in patch file not in the main file then I am unable to refer then using R.id.xxx.
So Please tell me if this is possible or not ?
If this is possible then how it can be achieved ?
I also want to know ,what are the resources or assets that can be added in the patch apk.
Speaking straight, It's not possible to separate the layout files from apk and then trying to access it using R.id.whatever
Expansion files are meant to store the assets such as media, docs, and other such static things which your application uses and the content of the expansion pack is stored on device's shared storage and is not linked with the apk file. To access the assets of the expansion file, you must code your application to read them from that device's shared storage location.
Coming back to the question of storing the layout files in an expansion file. To achieve this, you will have to write your own layout parser with the basic functionality similar to LayoutInflator. You can not use LayoutInflator to parse any layout file which is not part of the apk or stored on any external storage. So the idea is to store your layout files in the expansion file (which will be out of apk, on device shared storage), parse the file using your own parser, and add the views to the main layout at runtime. Though you wont be able to access your views using R.id.whatever but you can always use visual tree to access the views or while parsing the views store the referenced objects to access them for later use.
Hope this gives you a starting point.
I am trying to write a code that lets me programmatically switch a file from one of my resources folders to my assets folder. I have tried the usual file.renameTo method that has worked so very well for me in the past....but with android it seems to do nothing...
The assets and res folders are part of the structure of the .apk file. This isn't something that can be changed by the application on the phone. You can create a folder structure in the phone's file system, but that will need to be accessed from your app by the file access api, not as assets or resource identifiers.
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