I am new to Android application, create one xml file data.xml inside the Resource folder, then i tried to access that local xml file, but i can't get it.
But android resource folder and open rawresource to the raw folder under res inside xml file
data.xml using this way, i access that xml file like:
InputStream is = this.getResources().openRawResource(R.raw.data);
but using local xml file cannot access, please help me.
Thanks
raw xmls should be in Assets folder, and to access files from assets folder use:
AssetManager assetManager = getAssets();
InputStream in= assetManager.open("data.xml");
Add your data.xml file under raw folder.After inserting into it you will not get this sort of error.
Related
I am developing Kotlin app.
I am trying to read text and JSON files which I stored in resource folder(res/raw/file.txt).
Looking at the hierachy of app structure, I put absolute path(../../res/raw/file.txt) for file reader.
Unfortunately, my app cannot find the file.
Can you tell me how I can read files stored in res/raw? If I store file in wrong directory, can you tell me where to store and how to access them?
Thanks
If you want to read a file from res/raw folder you can obtain InputStream by R.raw.yourfile id like this:
resources.openRawResource(R.raw.yourfile)
Or you can open file by Uri:
val uri = Uri.parse("android.resource://com.example.your_package/raw/yourfile")
val file = File(uri.getPath());
Alternatevily you can put your files in assets/ folder and get InputStream like this:
assets.open("yourfile.txt")
I am creating an application where data are saved in .txt file in assets folder, I am trying to retrieve my data but I am not able to do, My teacher conditions to not to use SQLite and shared preference.
Place your text file in the /assets directory under the Android project. Use AssetManager class to access it.
AssetManager am = context.getAssets();
InputStream is = am.open("test.txt");
Or you can also put the file in the /res/raw directory, where the file will be indexed and is accessible by an id in the R file:
InputStream is = getResources().openRawResource(R.raw.test);
I have an XML File saved in Res->Raw , i want to get the path of this file to passed it in argument of function to parse it.
The name of XML file is families.xml , so I try this but doesn't work : "res/raw/families.xml". Any help?
Use :
InputStream is=getResources().openRawResource(R.raw.families);
to get an InputStream to your XML, and then use XMLPullParser to parse the file :
XmlPullParser xpp=XmlPullParserFactory.newInstance().newPullParser();
xpp.setInput(is,"your-encoding");
Another alternative would be to put it in the assets folder.
res/ is just source folder. It does not exist in final app. If you want to retrieve your file you may want to either put it in assets folder or use getResources().openRawResource()
I am trying to use org.apache.commons.io.FileUtils.readFileToByteArray(File) to read a text file from android assets into a byte array.
I cant seem to find the way to return a File Object from assets.
Any ideas?
If you want to create a file object from your assets folder you can do this :
File f= new File("file:///android_asset/","yourFile.txt");
i am developing one application for displaying images and labels and its sound.i want to get this info from xml .where i need to keep xml file in android project how to get values from that xml file
Thanks in advance
Aswan
You can package the xml file in assets/ directory inside your apk. With that, you can use
AssetManager assets = context.getAssets();
InputStream in = assets.open("myfile.xml");
If your file just contains labels, or strings, you can save it in put it in res/values/strings.xml inside your apk. In this case you can access the string values using the android generated R class
If you have a custom file (with custom structure) you can use DOM, SAX, or Xml Pull to read those files. e.g.
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in);