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")
Related
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 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.
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 have a binary file in the R.raw folder. Now I am able to
get the file name and can read the file as well, but I need to find out the complete
path of the file in string format.
any help will be appreciated.
So if you are able to get the file name you probably have a File object. If so call this method: http://developer.android.com/reference/java/io/File.html#getAbsolutePath%28%29
I think you should consider storing new files into your application data directory:
http://developer.android.com/reference/android/content/ContextWrapper.html#getDir%28java.lang.String,%20int%29