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");
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 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 need to check if a txt file exists on assets folder, and if exists I need to read the text of it and put into a string.
I saw some approaches on Google to open files from assets but I can't find one that gives you the possibility to check if the file exists and then read a string from it (because is a text resource).
You can just handle the exception properly when the file isn't found like this :
try {
InputStream is = SplashscreenActivity.this.getAssets().open("yourTextFile.txt");
} catch (IOException e) {
// catch your exception properly when the file isn't found
}
In short, you must already suppose to know if the file exists in assets or not. Because assets folder is packed up with files once you build your .apk file and can never be changed.
So, its pretty lame to verify if the file exists or not. It will only contain those files which you have added yourself.
More over, if you want to use R file to help you locate the file name, then simply put your files in a folder called raw. Its an alternate to assets but the files saved in this folder are referenced in the R class file - making it easier to get hold of the resource.
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 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