Android images in /assets or res/raw - android

If I wanted to put a bunch of images in a folder structure, e.g.. I’m building a beach app and each beach is displayed via my beach detail activity depending on the beach selected from my beach list. I then want to display five images from that particular beach. Each beach will have a folder with a name that corresponds to the _id of the beach and then five images in that folder, image1.jpg, image2.jpg… image5.jpg. should I put the folders and images in the /res/raw folder or the /assets folder for the best/easiest way to go.
Cheers,
Mike.

Assets will allow you to create subdirectories (group files in directories) note that they have no name restrictions however you can only access it via Assets Manager which contains list() for listing files under a given directory and open() for obtaining a file's InputStream.
resources instead, allow you to access the files in the folders but you cannot create subfolders, moreover you must place the files in the right place so bitmaps go in /res/drawable, layout xmls in /res/layout and so on. Resouces are easy to access for other parts of your code since they have an identifier.
Since you want to use the res/raw folder that means you cannot create a subfolder in it and that your images won't be compressed, so putting them without compress and considering they will be a lot of images then you app will be a bit heavy.
Here is two links that you might find helpfull: WiseAndroid and Providing Resources - Dev Guide

When adding images to your application you should place them in "res/drawable". That folder is for images that are not specific to any dpi level. For best results you should have three sizes of each image and place them in their respective dpi folders like "res/drawable-hdpi" for hi res images. This is a good section of the docs to read for resources and device compatibility.

Related

Fast copy subfolder from assets in Android?

I have seen many example in SO how to move files from assets. All of them copy files one by one, not as part of a sub folder, i.e. here.
Is it possible to move a sub folder to a target location? If I have more thousand small files then it seems faster, than treating each file independently.

android: How can I create subfolder in a `mipmap` folder, that can be accessed in code and layouts

I have my image resources in the various mipmap folders (mipmap-xhdpi, mipmap-xxhdpi, etc.) My project has hundreds of images (and growing) and it is becoming cumbersome that they are all in a single level in each directory.
I want to create a subfolder, such as "sprites" that I can reference in code with something like
R.mipmap.sprites.sprite_name
How can I create usable sub-folders in my mipmap folders?
When I create a new directory inside of a mipmap folder (using New -> Directory), the directory and its contents are invisible to anywhere that I am specifying a resource, namely:
• In the design-view of my xml layout, when I am browsing for an image resource for a background or an image button, the sub folders are hidden.
• In code, when I am specifying an image resource (R.... the subfolders are ignored).
Is there a correct way to do this? There appear to be some other directory options, Under the New -> Folder category, I have:
• Assets folder, described as "create a source root for assets to be included in the apk," but I don't want a root, I want a subdirectory.
• Res Folder, "source root for android resource files" 1. I don't see how this is different than an asset and 2. again, I don't want a root.
There are also several other folder types listed, that I am not familiar with.
If this cannot be accomplished, what is the correct way to include (and organize) hundreds (or thousands) of image resources in a project? Is the only way to do it really to have them all in a single directory?
How can I create usable sub-folders in my mipmap folders?
You can't. Resource directories of any time, mipmap or otherwise, cannot have subdirectories.
what is the correct way to include (and organize) hundreds (or thousands) of image resources in a project? Is the only way to do it really to have them all in a single directory?
If you are going to go with resources, then yes, though I would put them in drawable directories. mipmap is mostly for launcher icons.
the mipmaps are special because the app will display images of a different resolution depending on the device resolution
No. mipmap and drawable resources support resource resampling at runtime, but it is based on density, not resolution.
As ByteHamster notes, you can put whatever sort of directory structure you want in assets/, though you will need to put the appropriate values into BitmapFactory.Options to help with the resampling based on density.
Yes, you must keep all the resources in a single folder because, as you may know, different folders are used to target different device classes.
The right way to organise your resources in by prefix: as stated in the official Android documentation, you should use prefixes to make the resources appear in a meaningful order.

Upload images in Gridview from Assets folder via sqlitedatabase browser

I am working on a project where I am having an ExpandableListView populated with Items and SubItems. When I click on SubItems, I have to populate relative images into the gridView. All the images are saved in Assets folder and I have to put there path in an external SqliteDatabase which I have created using sqlitedabase browser.
So my question is, How to save Path of images in database from Assets folder.
BitmapFactory.decodeFile("file:///android_asset/image/test.jpg")
Though storing Images in the Assets is not the best way to go. You will not be able to take advantage of the Android resource management system. So unless you have a compelling reason to do this, I'd suggest that you take a look at using the res folder and the resources system.
BitmapFactory has a method to decode a file via the decodeFile method.
Android lets you access a file in the assets folder via the file:///android_asset/{path} path.
if your file in assest>image folder and your image name is test.jpg
then file:///android_asset/ is original path plus you need to mention remaining structure like file:///android_asset/image/test.png

Can I create folder for storing files besides RAW folder?

can i create folders, besides raw folder, for storing different kind of files?
For examples, my application need to count all the images put in a folder.
So, I need a specific folder for images only, and other folders for sounds, for .txt etc!
But when i tried to create a new folder, I couldn't reach it :(
It is just a hunch, but you might have more luck using the assets folder instead. You can certainly use it to store embedded web sites which have subfolders.
In general you cant create custom resource folders. But you can use assets dir and place your resources there. You can make as much subfolders in assets dir as you need. But android won't help you to manage your files in assets dir. Read this.

How to display any of 50 small images stored locally with application in a single dedicated folder

I want my program to be able to list all the 50 images that are in a local directory and access them at run time. I have tried to store them under the res folder, such as in Drawable, but have found out, according to one source, Ivan Memruk (here) that you can't refer to res folder subfolders by their folder names, because the folder names are all ignored -- you have to just use unique names for all your images and then refer to them by that name and using the whole R and resource id thing.
Also, according to that same article by Ivan, you can use subfolders under the assets folder, but then you can only access the images in them using an input stream, and (apparently) that is not of much use for accessing images.
Whoo, well all that said, does anyone have any other thoughts on an easy way to list all the images in a folder? I don't want to use the SD card by the way as my images will be shipped with the application.
Thanks in advance and thanks to the people who answered before I updated my question.
Store the images in drawables folder. But maintaining their identifiers (R.drawable) is a problem. You can get the identifiers anytime from :
getResources().getIdentifier(imageName,"drawable", context.getPackageName());
This will return R.drawable value of image.
The advantage of storing images in drawable folder is you can store resolution specific and orientation specific images.
You may use the assets folder for this purpose, remember not to name the images folder within assets as images, otherwise some extra images will come up in the image list when asked for. Instead try to give a unique name like app_images, etc.

Categories

Resources