I have a few files as resources in the android app.
On the other hand I have some methods that require a "File" object as an argument.
How can I bridge the two?
Edit: I need some way to have a "File" object and not to read the file as an inputstream. thank you!
You could copy the file to external storage (reading from your raw resource's InputStream and writing to a new file) and then open it as a File from there. That's kludgy, but I'm pretty sure there's no way to get the resource as a File directly.
Related
After searching for solution, I have found from this link that it is possible to retrieve path like:
Using Resource Id
Syntax : android.resource://[package]/[resource_id]
Example : Uri.parse("android.resource://com.my.package/" + R.raw.mp3filename);
Which is exactly what I want. Problem is that, I'm using lolipop, and it does not work. When the package/resource is parsed, path from Uri.parse will be null. Is there other way around to write this, since in my Song class, I have only access to resourceId (R.raw.mp3filename).
Is it possible to get file path by using R.raw.mp3file
No, because it is not a file on the device. It is a file on the hard drive of your development machine. On the device, it is merely an entry in an APK file.
Either:
Do whatever you are trying to do some other way that does not involve a file path, or
Use openInputStream() on a Resources object (you can get one from any Context via getResources()), and use Java I/O to copy the contents of that resource to a local file, such as on internal storage
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 have a file stored in res/raw, and I would like to know how to wite into that file.
I tried getResources but it returns an inputStream.
Not possible. If you need to copy an asset to the device store the file in the assets directory. Then use the AssetManager to stream in and copy the asset so you can write it to disc or application cache.
You shouldn't be writing your application resources. Instead, try copying the resource to your writable data directory and modifying it there. There a number of methods on Context that will help you find an appropriate directory, depending on your needs.
I must get a File object or a Uri object from android assets directory, but I only know how to get inputStream object.
Is there any other way to do it?
You cannot get a File object to an asset, as it is not a file on the device. It is a file in your development machine, but it is merely an entry in an APK file on the device.
Some things, like WebView, can work with a file:///android_asset/... Uri, where ... is replaced by the relative path within assets/ of your project to the file of interest. For example, if your project has assets/index.html, file:///android_asset/index.html would point to that file. AFAIK, not everything can use this sort of Uri, and in particular things that need a URI (e.g., java.net) probably cannot use it.
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.