Where to store many subofolder locally images android - android

I have a series of images (200-300) of animals that will be loaded inside my application.There is a root folder and subfolders containing 3-4 images to any type of animal.
The application must work in local.Where I recommended to save these pictures? In assets or drawable directory ? Or something else ?
Thanks in advance

You should store your images in drawable folder.
You can create sub folders in assets dir. and call it with reference to the folder / dir structure.
I'm not sure though you can do it with drawables or not as never tried it but you can give it a try. Rest follow the below arrays approach to manage your code better.
Plus if you want to categorize them then care different arrays for each category and put the resource reference in each one of them accordingly. Then you can user these category arrays according to your needs.
e.g
String dogs[] = {R.drawable.dog1, R.drawable.dog2, R.drawable.dog3, etc};
String cats[] = {R.drawable.cat1, R.drawable.cat2, R.drawable.cat3, etc};
and you can use these arrays in their own category.

Related

Can there be a content directory, where I can put files (compile time) and load files from (run time), inside an app?

I have some image files which will be dynamically loaded in my app. I know I can put images in the "/res/drawable" directory, but can there be somewhere else I can put files and loaded them by file name at runtime?
The image files contain characters that are now allowed in resource names. I did not name them, so ideally, I need to keep the names. For example, suppose I have "Cat #1.png" and "Cat #2.png", and I would like to load the image into an ImageView by that name at runtime, like so:
MyImageView1.setImageBitmap(createBitmapFromContentDirectory("Cat Pictures/Cat #2.png"))
MyImageView2.setImageBitmap(createBitmapFromContentDirectory("Dog Pictures/Dog #1.png"))
Is that possible, or should I change all image names into conforming names (e.g., cat_number1.png and cat_number2.png) and put them into the drawable directory?
You can put them in assets & load them from there,
Otherwise you can also put them in raw folder.
you can refer to Where do I I Place assets folder for assets.assets doesn't create Resource Id, so you can access them directly. Have a look at this
difference between assets & raw.

keeping images in a categorized way in android drawable folder and how to access them

I have almost 1000 images with different categories like
Students_Images_Folder
Teachers_Images_Folder
Parents_Images_Folder
etc.
I want to keep import these images in my android project with categories not mdpi, xdpi,hdpi etc . Is there anyway to keep them categorized not merged in drawable.
Should I go for database ? I read that database is not suitable as it will lower efficiency. Any solution is welcomed. thanks for giving time to my silly question.
First of All you should add all images into drawable folder with unique name and of course some prefix like st for Student, tr for teacher and pr for parent.
Secondly you can create database with three tables to store the names of images into it individual category wise.
After that you can retrieve the images category wise from database and here is code to deal with resource id from file name.
OR
You can also create assets folder in android, here you can see how to create assets folder in android studio.
After creating assets folder you can create sub-directories according to your categories like student, teacher and parent.
And after than put images inside those directories.
Lastly load images from assets. To load images from sub-directories of assets implement following code
try
{
InputStream ims = getAssets().open("student/st_img.jpg");
Drawable d = Drawable.createFromStream(ims, null);
mImage.setImageDrawable(d);
ims .close();
}
catch(IOException ex)
{
return;
}
You can make subdirectories in your resource directory and make your build system use it with a small piece of Gradle script.
Read here for more information: Can the Android Layout folder contain subfolders?
Or this blog here: Android: Multiple Resource Folders
AFAIK, you can't add subdirectories to res folders, this will cause the resources compiler to fail!
But you can add a prefix at the beginning of imgaes's names for each category, for example: st_image_name for students images

Where are the resources' IDs?

Android will allocate ids for each pics in the res/drawable directory. Where are they?
I want to dynamically choose one pic form the pool and show it. How can I do that?
They are stored in the res/drawable folder.
If the file name is demo.png, then they can be accessed by R.drawable.demo
If you want to access a random drawable, store all the resource identifiers in a Integer ArrayList, and programatically generate a random function using Random(), and get that particular item from the arraylist. Then you'll have a random drawable every time.
Autogenerated ids are in the gen file, but not advisable to use them. It would be better to use the filenames directly through some predefined array of R.drawable.filename and randomly pick them.
There IDs are stored in the R.java file, but you cannot edit it, as your changes are over written each time.
You can also access resources by name, which may be a viable approach to solving your problem if you know the names of the resources or can derive them according to some pre-defined naming scheme. (for example images are named in the sequence image1, image2 and so on.
You have to map the name to the identifier using the getIdentifier() method of the Resources class.
String name = "resource" + rng.nextInt(count);
int resource = getResources().getIdentifier(name, "drawable", "com.package");
The documentation for this method says:
Note: use of this function is
discouraged. It is much more efficient
to retrieve resources by identifier
than by name.
This is true but need not be a problem if you are doing it in code that isn't performance sensitive.
Alternatively, if you don't mind listing the resources in XML, you could create a typed array that you can then randomly select from.

Sorting Images on Android

I need images to be sorted by folders. like drawable/Photos, drawable/items, drawable/photos. and put each folder to the registry or at least find way to get them out of there. all I want is something close to php like "../img/photos/photo0.jpg". is there way to do something like this. and folder with Images must contain in apk file.
possible solutions is make link in the R file but I didn't find how to do it, other solution is find command with logic like I will show you here:
ImageView img = (ImageView)CloningTable.findViewById(R.id.img);
String ImgPath = "com.test.test/img/img0.jpg";
img.setImageDrawable(Drawable.createFromPath(ImgPath));
OR
ImageView img = (ImageView)CloningTable.findViewById(R.id.img);
String ImgPath = "com.test.test/img/img0.jpg";
img.setImageResource(ImgPath);
please say me the best way to handle it. AND specify if it contain path how I can know path the file lies in.
File ImgFile = new File("test/test.jpg");
TextView testtext = (TextView)CloningTable.findViewById(R.id.testtext);
if (ImgFile.exists()) {
test.setImageBitmap(BitmapFactory.decodeFile(ImgFile.getPath()));
testtext.setText("asdasuidjasdk");
}
can any one say Why programm can't find file and file exist 100% = /?
filepath: Project> assets/test/test.jpg
found solution: Android Show image by path
It's hard to understand why you need to sort resources that will be static in your APK, but you should not access them directly using paths, but using the API for that purpose.
Take a look at the topics in the dev guide here:
http://developer.android.com/guide/topics/resources/accessing-resources.html
http://developer.android.com/guide/topics/resources/drawable-resource.html
Those will teach you how to access the resources. To create Bitmaps from them, the easiest way is to use the BitmapFactory class
Remember, you know which resources the APK has at building time, so you can work around it. If you want to work with Bitmaps created at runtime, then use the data storage methods instead.
In this case, you should put resources in assets folder, and you can access them by path!
So if you want to save the path string, the best way is creating a class, with 2 variants: 1 Bitmap to store data, and 1 String to store the path.
If not, just create a String array that you may access when you want.
In both case, you need to put the path into the Path String storage before you load the image.
Hope I understood your question.
EDIT: Final answer that meet your requirement:
There's no path for drawable, they are all converted into ID. If you put in asset folder, just call it by their name. For example, you have "Test.bmp" in asset folder, its path is "Test.bmp". If you have "SubTest.bmp" in "TestFolder" in asset folder, its path is "TestFolder/SubTest.bmp". Sorry for long time, I had to sleep, it was mid night at my time zone.

custom Resource folder names for layout,drawable .. in android

I have 2 different functionality to be implemented as part of a single application.
They have different screen flows and BL to be implemented but a single apk,so it is transparent to the end user.
I have 100 screens for each of the flow,so can I have custom folder names in the resources directory for easy maintenance and loose coupling.
eg: res
-layout
-layout_savings
-layout_checking
-drawable
-drawable_savings
-drawable_cheking
currently we have only the following structure
res
-layout
-layout-land
-drawable
-values
Will there be any problem while generating the R.java file.
Any help would be appreciated.
for what you need , i suggest you to use the assets directory instead of res. in assets dir you can sort your files anyway you like (dir name/filename) . You access them with AssetManager.
this require a little change in code but i think this is the only way if you want to sort your file like that , res folder doesn't allow custom directory names.
(Or of course you can store files in the sdcard)

Categories

Resources