I have a few thousand image files in a directory structure. There are a few hundred different subdirectories and each directory contains 10 images named 1.png - 10.png...
How can I add these folders to my app and access these images? Assuming I want foo/bar/1.png?
In android it doesn't appear like you have a string path to your assets or raw directories so I have no clue how I would do this. Please don't tell me I have to zip them then unzip them out of assets.
In android it doesn't appear like you have a string path to your assets
Sure you do. getAssets().open("foo/bar/1.png") will give you an InputStream to that PNG file, assuming that your project has assets/foo/bar/1.png in it. You can then use BitmapFactory to read it in.
However, resources cannot exist in subdirectories. You could write a script on your development machine that flattens the structure (e.g., foo/bar/1.png into foo_bar_1.png), then put the renamed files into whichever res/drawable-NNNN/ directory is appropriate.
the path to access the android asset folder is :
file:///android_asset/filename
You should be able to access a folder with :
file:///android_asset/folder1/folder2/targetFolder/
Note that there is 3 '/' after 'file:'
Related
I make android application that read file.txt from resource/raw by context?.resources?.openRawResource(R.raw.file), and it's work fine.
I have tried assets directory by : context?.assets?.open("file.txt") and it's work also.
Now, I need to delete this raw/assets resource file or delete the content. Is there anyway to do it with android code?
No, there is no way to update the Assets or Raw files.
Both of them are read-only.
What you can do, it's to make a copy of the file in a writable directory and modify them as needed.
UPDATE
Just to give more context, both assets and any resources are part of the binary file (APK or app bundle). As they come as part of the package you cannot change them without creating a new binary.
I have some files that I want to read from my app.Currently, I have to do the following:
Check if the file exists in the files directory.
If it doesnt, then copy the files from assets to the files directory
If it does, then, skip step 2
So, is it possible to not copy the files ? This increases the size of my app (uselessly) as there is an extra copy of all the files.
Note, that I have to access the file using getfiles(). I am using a library that doesnt work if I give the uri of my assets folder.
So, it it somehow possible to compile the app with some files already in the files directory ?
Internal storage for you app is created when application is installed so there is no way to provide files there during compile time, that would be magic.
You could try creating ContentProvider for sharing your files stored inside assets folder.
Besides, you should tell which library you are using. Then I may be able to suggest something more precise.
I want to add a text file to my APK root. This file will not be used in the application but it will stay there for manual extraction of the APK.
I tried to put it into the root of the project in Eclipse but it didn't include that file in the APK. I don't want to put it into assets folder. Can't I put it to the root?
Use the assets folder.
According to Android developer
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.
Files that are not put into any of the main directories of your android project will NOT be included in your apk. You MUST put the file in the assets so it can be accessed within the application as an external resource/file.
I have a directory which contains some files. I want to bundle this directory inside apk file so that when I install apk file the directory gets copied to a particular location on the android device.
Can I bundle a directory inside apk file as I didn't find this information on developer.android.com? Do I need to modify any configuration file for this?
Thanks..
Ajay
You cant, but you could take a look at the android assets folder,
http://developer.android.com/guide/topics/resources/providing-resources.html
Assets are raw resources, they won't be compressed by the android compiler.
From the developers site:
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 the AssetManager. For example, this is a good location
for textures and game data.
The assets/ folder in apk is intended to store any extra user files in any formats. They will be included to apk automatically on compilation stage and can be accessed from the app using getAssets() function.
I just want the path of the assets directory which I want to use as a variable throughout my app.
I know we can refer a file in the "assets" directory using AssetManager, but I don't want to refer a file as of now, I just want the path of the assets directory.
Can anybody help me with this?
I just want the path of the assets directory which I want to use as a variable throughout my app.
There is no "path of the assets directory" on the device.
I know we can refer a file in the "assets" directory using AssetManager, but I don't want to refer a file as of now, I just want the path of the assets directory.
There is no "path of the assets directory" on the device.
Can anybody help me with this?
No, because there is no "path of the assets directory" on the device. Please use AssetManager to retrieve an InputStream to your assets as needed.
Since android has a file size restriction on files inside raw directory, I will be placing my files in assets directory, and during runtime, I would be retrieving them as need
The "restriction" is the same for assets, AFAIK. The usual way to circumvent the "restriction" is to give the file an extension that Android's build tools will not try to compress (.mp3 works IIRC).
Try using
AssetFileDescriptor