【问题标题】:How to get specific images Resource from Assets Folder?如何从 Assets 文件夹中获取特定的图像资源?
【发布时间】:2017-07-10 18:44:40
【问题描述】:

我在assets文件夹下有8张不同的植物种类图片,我在数据库的assets目录下缓存了8张图片assets文件名(对应路径,例如foxtail.png,orchid.png)。 (加上其他信息)

我在 RecyclerView 中显示 8 种植物。单击任何植物都会打开详细信息活动。 (传递保存在资产文件夹中的图像文件名,例如 foxtail.png)

如何在 assets 文件夹中选择与传递给 Detail Activity 的文件名匹配的特定图像文件并将其设置为 ImageView??

【问题讨论】:

    标签: java android imageview resources assets


    【解决方案1】:

    你可以:

    以流形式打开文件

    InputStream imageStream = null;
    try {
        // get input stream
        imageStream  = getAssets().open("foxtail.png");
        // load image as Drawable
        Drawable drawable= Drawable.createFromStream(imageStream, null);
        // set image to ImageView
        image.setImageDrawable(drawable);
        }
    catch(IOException ex) {
        return;
    }
    

    最后记得用

    关闭流
    if(imageStream !=null){
        imageStream.close();
    }
    

    将图片移动到 res/drawable 文件夹中您可以使用以下方式加载图片:

    String yourImageName = getImageNameFromDB();
    int resId= getResources().getIdentifier(yourImageName, "drawable", "com.example.yourpackegename.");
    ImageView image = (ImageView)findViewById(R.id.image);
    image.setImageDrawable(resId);
    

    使用类似的东西(总是将图像放入 res/drawable):

    private enum Plant {
        foxtail, orchid, xyz;
    }
    
    String value = getPlantFromDB();
    Plant plant = Plant.valueOf(value); // surround with try/catch
    
    switch(plant) {
        case foxtail : 
           resId= R.drawable.foxtail
           break;
        case orchid : 
           resId= R.drawable.orchid
           break;
        default : 
           resId= R.drawable.xyz
           break;
    Drawable drawable = getResources().getDrawable(resId);
    ImageView image = (ImageView)findViewById(R.id.image);
    image.setImageDrawable(drawable);
    

    【讨论】:

      【解决方案2】:

      您可以创建一个包含资源 ID 的 int 数组。

      int images[]={
              R.drawable.image_1,
              R.drawable.image_2,
              R.drawable.image_3,
              R.drawable.image_4,
              R.drawable.image_5,
              R.drawable.image_6,
              R.drawable.image_7,
              R.drawable.image_8
       };
      

      在您的数据库中存储与资源数组中的图像位置相对应的图像 ID。

      | id | image_id | information |
      -------------------------------
      | 0  |   2      |   info_0    |
      | 1  |   0      |   info_1    |
      | 2  |   4      |   info_2    |
      

      所以当你从数据库中获取行时,你可以使用 image_id 从数组中检索对应的图像

      ImageView imageView = (ImageView)v.findViewById(R.id.imageView);
      imageView.setImageResource(images[image_id]);
      

      【讨论】:

      • 谢谢,如果图像在可绘制文件夹中,这是可能的,但所有图像都保存在 assets/ 目录中。我如何阅读/引用它们并显示它。
      • 你不能保证id永远保持不变
      【解决方案3】:

      使用 Resource Drawable Id 在每个 ImageView/View 上设置标签,例如。 R.drawable.foxtail

      例如。 imageView.setTag(R.drawable.foxtail)view.setTag(R.drawable.foxtail)

      当一个被选中时,获取标签并将其发送到下一个活动:

      然后再次检查例如。

      imageTag = getIntent().getIntExtra("chosenPlant");
      
      if ( imageTag == R.drawable.foxtail ){
          //Perform action if this pic was selected (foxtail.png)
          newImageView.setImageResource(R.drawable.foxtail);
      } else ...
      

      【讨论】:

      • 图片不在android资源文件夹中,保存在assets/目录下,不能像R.drawable.foxtail那样引用
      • 你能把它移到drawable文件夹吗?这将使过程更容易
      猜你喜欢
      • 1970-01-01
      • 2020-06-22
      • 2022-08-23
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多