I want to use RandomAccessFile in my app and one of the requirenment is to keep my files inside app. So I'm able to read files from resouces, but in case of using RandomAccessFile I need String or File instance to work with it.
And As I know Is not possible to get absolute path of asset file or of file in raw folder. Maybe some workaround solution is here? How to read resource file using RandomAccessFile?
Related
Where do I put a text file that I want deployed with my app such that I can provide within my app a path to that text file. I have an included JNI library which will take the text file at that path and perform actions on it. So in other words, I don't think I can just put it in my assets folder without reading the file and resaving it to SD card or something (as I don't think you can reference an assets file directly by path right?). Is there a way around this?
You can place files in res/raw/ and read them as raw resource files/streams.
See https://stackoverflow.com/a/15934525/5734895 for reading raw resources.
I would like to port some code to Android(more pecisely an XML parser net.xqhs.util.XML ) in order to do that I need to read files from assets or raw folder using RandomAccessFile.
How can I do this?
Thank you!
That is not possible, sorry. Neither an asset nor a resource is a file on the Android device, and so while you can get an InputStream on them, you cannot create a RandomAccessFile on them.
Your choices are:
Use any of the available XML parsers for Android that take an InputStream. There are three XML parsers that are part of Android (DOM, SAX, XmlPullParser) that can work with streams. I would expect most of the third-party XML parsers to also support InputStream.
Copy the asset or raw resource (e.g., using an InputStream) to a local file. Then, create a RandomAccessFile for that file.
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.
My program needs large .txt file to be stored on SD-card (so, I want to redistribute it with .apk, without creation from the program). How I can attach the file (created on PC) to .apk?
You can save it in /res/raw folder
If you save your file as yourfile.txt
InputStream inputStream = this.getResources().openRawResource(R.raw.yourfile);
As far as I know you can open a .apk with zip and add files. Where is no magic :)
It might be slightly easier to use the AssetManager. Just save your files in the assets/ directory (as opposed to res/raw/). Then you can access them directly using their filename. http://developer.android.com/reference/android/content/res/AssetManager.html describes how to access the assets.
Is it possible to add ZIP file to APK package as a raw resource and read it with ZipFile class? It looks like it's trivial to open file from SD card, but not from APK.
I don't believe so. As you alluded, it's easy to use ZipFile with an actual file on the file system, but not so for a file embedded in your application.
For accessing a zip file in your APK, you'd have to use something like Resources.openRawResource(R.raw.zip_file) and then wrap the returned InputStream in a ZipInputStream and do it the usual Java way, rather than with the Android method.
Alternatively, if you really find you need to use the ZipFile class, you could extract the zip file from the APK to the SD card or, more reliably, to your application's local storage.