I am trying to make a file containing the folder res/raw so I can use listFiles() so I can index all the files in that folder... I tried using
File folderDirectory = new File("/data/data/com.soundboard/res/raw/");
file[] soundFiles = soundDirectoory.listFiles();
ListFiles returns null, I think, unless I am reading my debugging info wrong (the soundFiles path is null)
What am I doing wrong?
You could use cd /data/data/com.soundboard in adb shell to find there is no res/raw there.
Usually, res/raw in .apk should be static, so you could unzip .apk simply and find out what are in res/raw.
res/raw can not be opened as a File and used as a directory. There is no way to index the files in that folder.
EDIT: when you build your APK the contents of the res/raw directory are compiled into an efficient format. See How does the mapping between android resources and resources ID work? for more detail. The best you might be able to do is use reflection on your packages R.raw class and list all its fields: I've never seen this done, so whether it will actually work is anyone's guess.
Sounds like you should be using /assets and the AssetManager. It behaves exactly like a file system, so you can use list. The /assets and /res/raw serve much the same purpose, with the one big exception is that /assets can be accessed as a file system.
Otherwise as Femi mentioned, you could use reflection and get the fields for com.soundboard.R.raw. You could then iterate over those and generate file descriptors for each file there.
Related
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 am really new to android devices, we have got an app that needs to read a *.cfg file from the same place as the apk. So the question is:
1) When i attach my mobile-phone to the computer, can i put it from the explorer? If yes, how?
2) Is there a mechanism in Android, that looks for some kind of include directories
when opening a Program?
It would be great if you could give me a hint.
You'd want to drop the *.cfg files into the assets/ folder. Then the app will have access to those raw *.cfg files during runtime.
From Managing Projects
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.
I am working on reading epub3 file and i want to access all files which used for create epub3 without *unzip it.*
any suggestion or solution would be appreciated....
I have tried to solve this and i got the following solution.
Zip is a one type of file Which include many directory and files so we can't get it in directory and file format.
We can only get it in Bytes. Which is unused.
So we Have To Unzip it.....
Because Whenever we unzip it, then itself make one type of directory of sub directory and files So then we can access it in directory and file format.
While you can technically read the bytes from the zip archive without decompressing them, it will not help you as the data will be unusable in any epub display.
When a file is zipped, it is compressed using compression tables. This alters the data of the file, and to get back the original data you must decompress, or unzip, it first.
This is virtually no way to access files without unzipping. However, there is a way for targeted unzipping, i.e. unzipping a specific file instead of entire zipped folder. If that sounds good, you could probably go for Objective zip project.
P.S: Even i was working on a epub3 reader and I had to unzip the entire folder. Let me know if you find a better work around.
I'm very new to android. I would like to create a package that upon install would put files in the app folder (/data/data/my.app/...) of the android file system.
I would like to do this, so I could access the files using standard java File methods (in order to reuse my own code), and not have to use the android resource accessing methods.
The only way I thought might achieve something in this direction, is to put the files in the /res folder, and copy them to the file system in runtime (on first run, for example). This has the disadvantage of having two copies of each file for no reason.
Another way could be to get my app to download these files from a server (saw this option in another answer as well). This is possible, but I would wish to avoid having to put up a file server for such a simple task...
To my understanding, DDMS will not help here, as it only allows me to access the folder manually, after the application is installed.
My question is: Is there a different way to achieve my original goal (accessing files using standard java methods)? If not, is there a cleaner way to put files on the file system?
Thanks!
There are 3 methods I can think of that avoid duplication:
As you mentioned using the res/raw folder. You can avoid duplication if your existing code uses can use InputStream instead of File and use Resources.openRawResource().
Use the assets folder and access the file using file using AssetManager.openNotAssetFd(). Again this would require the use of Streams not File class. This can get a bit messy if your file is compressed because assets are memory mapped.
If you code is really tied to File (doesn't just use it to open an InputStream). To avoid duplication you could download the file from the web on the first time it is run and store it into the external storage.
You can put your files on under /res/raw and then use:
Resources res = context.getResources();
InputStream in_s = res.openRawResource(R.raw.myfile);
Or if you want the File object
File f = new File(context.getResources().openRawResource(R.raw.myfile));
Then you can use normal Java apis to work with your files.
I am trying to load an xml file located in the /assets folder of an android project by name using this method:
getAssets().openXmlResourceParser("thefilename.xml");
However, executing this code always throws a "FileNotFound" exception, even though the file is located in the /assets folder and is with the correct file name.
Now, I have not put the file in the /res/xml folder because I really need to be able to 1. edit the file right on the device itself and most importantly 2. add new xml files to the application without issuing an update, to allow for easy user modifications.
Thanks in advance.
I think what you are looking for is either getAssets().open("thefilename.xml") or getAssets().openFd("thefilename.xml") depending on what the end use of the file is. You can see from Dianne's response in this post awhile back that openXmlResourceParser() is not really usable just to gain access to files in the assets/ directory: http://goo.gl/2KfgT
From there you will have a stream that you could feed into a SAXParser or do whatever else you choose.
Side Note: On the points you mentioned you really can't edit files directly in assets/ or add new files to assets/ at runtime. You will need to work with files on either internal or external storage to do those things. You probably already knew that, but I thought I'd mention it.
Hope that Helps!