简体   繁体   English

将图像以分类方式保存在android drawable文件夹中以及如何访问它们

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

I have almost 1000 images with different categories like 我有近1000张具有不同类别的图像,例如

  • Students_Images_Folder 学生_图片_文件夹
  • Teachers_Images_Folder Teachers_Images_Folder
  • Parents_Images_Folder etc. Parents_Images_Folder等

I want to keep import these images in my android project with categories not mdpi, xdpi,hdpi etc . 我想继续将这些图像导入我的android项目中而不是mdpi,xdpi,hdpi等类别 Is there anyway to keep them categorized not merged in drawable. 无论如何,要保持它们的类别不合并在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. 感谢您抽出时间回答我的这个愚蠢的问题。

You can make subdirectories in your resource directory and make your build system use it with a small piece of Gradle script. 您可以在资源目录中创建子目录,并使构建系统通过一小段Gradle脚本使用它。

Read here for more information: Can the Android Layout folder contain subfolders? 在这里阅读更多信息: Android布局文件夹可以包含子文件夹吗?

Or this blog here: Android: Multiple Resource Folders 或以下博客: Android:多个资源文件夹

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. 首先,您应该将所有图像添加到具有唯一名称的可绘制文件夹中,当然还要加上一些前缀,例如st代表Student,tr代表老师,pr代表父母。

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. 之后,您可以从数据库中明智地检索图像类别, 是用于处理文件名中的资源ID的代码。

OR 要么

You can also create assets folder in android, here you can see how to create assets folder in android studio. 您还可以在android中创建资产文件夹, 在这里您可以查看如何在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;
}

AFAIK, you can't add subdirectories to res folders, this will cause the resources compiler to fail! AFAIK,您不能将子目录添加到res文件夹,这将导致资源编译器失败!

But you can add a prefix at the beginning of imgaes's names for each category, for example: st_image_name for students images 但是,您可以在每个类别的图像名称的开头添加前缀,例如: st_image_name用于学生图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM