0

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.

Muhammad Ibrahim
  • 229
  • 4
  • 16

3 Answers3

2

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

Community
  • 1
  • 1
Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
2

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;
}
Community
  • 1
  • 1
sodhankit
  • 1,138
  • 1
  • 14
  • 26
1

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

Atef Hares
  • 4,715
  • 3
  • 29
  • 61