【问题标题】:How to find Image from raw folder inside res如何从 res 内的 raw 文件夹中查找图像
【发布时间】:2016-08-23 13:13:37
【问题描述】:

我有四个 Google、Yahoo、GMail 的图片,如果有人在编辑文本 Google 中输入然后设置 Google 的 imageview,我想要。

为此,我在 res 文件夹中创建了一个原始文件夹,其中包含所有图像,并且有人在编辑文本中输入它包含在 String s 中(如 s="Google"),但我不知道如何进一步设置图像

【问题讨论】:

标签: android


【解决方案1】:

你可以通过这种方式获取Image as InputStream

InputStream ins = getResources().openRawResource(
            getResources().getIdentifier("image name",
            "raw", getPackageName()));

【讨论】:

  • 先生,如果用户同时选择 Yahoo 和 Google,我如何通过输入流获取多个图像。基本上我正在制作图像和名称的网格视图,所以我怎样才能获得多个图像
  • 将选定的变量添加到arraylist并使用forloop执行您需要的操作。
【解决方案2】:
 int identifier = mContext.getResources().getIdentifier("file_name","raw", mContext.getPackageName());
    if(identifier>0){
        imageView.setImageResource(identifier);
    }

【讨论】:

  • R.raw.image_name 有什么问题?查找资源名称很慢
  • 请不要只写代码,解释一下,这样OP可以从你的答案中学习。
【解决方案3】:

像这样从原始文件夹中加载图像:

if(etText.equals("Google")    {
    InputStream imageIS = this.getResources().openRawResource(R.raw.google);
}
else if(etText.equals("Yahoo")    {
    InputStream imageIS = this.getResources().openRawResource(R.raw.yahoo);
}
else if(etText.equals("GMail")    {
    InputStream imageIS = this.getResources().openRawResource(R.raw.gmail);
}

Bitmap bitmap = BitmapFactory.decodeStream(imageIS);

然后将其加载到您的图像视图中:

 imageview.setImageBitmap(bitmap);

【讨论】:

    【解决方案4】:
    int imageArray[] = {R.drawable.google, R.drawable.yahoo, R.drawable.gmail};
    
    //onClick
    String etStr = edittext.getText().toString();
    
    if(etStr.equals("Google"){
         imageView.setImageResource(imageArray[0]);
    }else if(etStr.equals("Yahoo"){
         imageView.setImageResource(imageArray[1]);
    }else if(etStr.equals("GMail"){
         imageView.setImageResource(imageArray[2]);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多