I'm trying to develop an application for android...
The question is, I need to Edit or create(if not existing already) an XML file in the Resources folder or Assets folder(anyone will do)...How can I do that?
I can get the XML file to open from the Assets folder using the
getAssets().
But I need to modify its contents and store it again somewhere....
I don't want to save it on the memory card....Please Help...
I hope you guys will do....Thanks in advance...
The question is, I need to Edit or create(if not existing already) an XML file in the Resources folder or Assets folder(anyone will do)...How can I do that?
You don't. Assets and resources are read-only at runtime.
But I need to modify its contents and store it again somewhere
Store it in a file using Java file I/O.
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.
I am writing a simple Android Sudoku app, during the first stage, I want to store some Sudoku puzzle in a file and then get them. The file is a simple xml file, it's format just like:
<puzzle>23000...31</puzzle>
<puzzle>45013...67</puzzle>
There are 9 * 9 character in "puzzle" tag.
My question is, what's the correct directory for the file, and how to get the data from it?
You can store your file in assets directory or even raw directory. Honestly, I would advise you to use the first solution: you will then be able to use inside your code:
getAssets().open(fileName)
"assets" directory has to be at the root of your project (same level as ""src", "res", "libs"...
"raw" directory has to be inside the "res" one.
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).
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
how we can remove a file from the assets folder at runtime in android?
Is it possible to create a file in assets folder when an application is run and delete the same file when the app is stopped.
For asset you have read file permission only. So you cannot create / delete files in assets folder. Following are the restrictions applied to assets folder.
1) Only Read Permission available for this folder.
2) File size for placing inside the assets limit to 10 MB.If you want to place
then the larger file should be placed as chunk.
3) You can use this folder, for placing files like database file, font file etc...
Don't fight with it. It is not a proper way of dealing with files using assets folder.
You can use SDcard that is suitable location for creating and deleting files
You can use standard method for placing files in SDcard through app,
Environment.getExternalStoragePublicDirectory("filetype");
The apk is read only and the assets/ folder is not modifiable at runtime, which sounds like something you may be seeking. Check this question
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!