2017-03-05 34 views
0

使用可绘制的图像我收到的图像链接从API,如: “http://host/skin/images/answer_picture/image_1.png存储在应用程序与吉尔德

但我想直接从应用程序使用的图像,因为许多用户都加载图像问题。

因此,我将图像添加到应用程序中,并更改API以仅获取图像名称“image_1.png”。

旧的代码为:

  View txtOption; 

      if (AppConstant.arQuestionList.get(questionid).getArAnswer().get(i).getAtype() 
        .equalsIgnoreCase("image")) { 
       txtOption = new ImageView(this); 

       try { 

         Glide.with(QuestionActivity.this).load(AppConstant.arQuestionList.get(questionid) 
          .getArAnswer().get(i).getAnswer()).override(60, 60).into((ImageView) 
          txtOption); 

       }catch (OutOfMemoryError e){ 
        e.printStackTrace(); 
       } 
       txtOption.setPadding(5, 5, 5, 5); 

      } else { 
       txtOption = new TextView(this); 

       ((TextView) txtOption).setText(AppConstant.arQuestionList.get(questionid) 
         .getArAnswer().get 
           (i).getAnswer()); 
       ((TextView) txtOption).setTextColor(getResources().getColor(R.color.answer_color)); 
       ((TextView) txtOption).setTypeface(tf, Typeface.BOLD); 
       ((TextView) txtOption).setGravity(Gravity.CENTER); 
       txtOption.setPadding(10, 20, 10, 20); 
      } 

新的代码(不工作):

    // get the img name example: Skill_test.png 
        String imgName = AppConstant.arQuestionList.get(questionid) 
          .getArAnswer().get(i).getAnswer(); 

        // delete image name extension 
        if (imgName.endsWith(".png")) { 
         imgName = imgName.substring(0, imgName.length() - 5); 
        } 

        String uri = "drawable/"+imgName; 


        int idDrawable = getResources().getIdentifier(uri,"drawable", getPackageName()); 

        Drawable drawable = ContextCompat.getDrawable(QuestionActivity.this, idDrawable); 

        Glide.with(QuestionActivity.this) 
          .load("") 
          .placeholder(drawable) 
          .into((ImageView)txtOption); 

回答

0

最后我想通了,什么是错误

    // get the img name, example: Skill_Hero_Skill-Name.png 
        String imgName = AppConstant.arQuestionList.get(questionid) 
          .getArAnswer().get(i).getAnswer(); 

        // delete image name extension 
        if (imgName.endsWith(".png")) { 
         imgName = imgName.substring(0, imgName.length() - 4); 
        } 

        // string to lowercase 
        imgName = imgName.toLowerCase(); 

        // replace "-" for "_" 
        imgName = imgName.replace('-','_'); 


        String uri = "drawable/"+imgName.toLowerCase(); 

        int imageResource = getResources().getIdentifier(uri,"drawable", getPackageName()); 


        Glide.with(QuestionActivity.this) 
          .load(imageResource) 
          .crossFade() 
          .override(60, 60) 
          .into((ImageView)txtOption); 
0

只需使用:

int idDrawable = getResources().getIdentifier(imgName, "drawable", getPackageName()); 

Drawable drawable = ContextCompat.getDrawable(context, idDrawable); 

Glide.with(context) 
     .load("") 
     .placeholder(drawable) 
     .into(imageView); 
+0

对不起,如果这个问题有点愚蠢。 getDrawable无法使用上下文,并且imgName因为无法读取字符串。 另外.into(txtOption)说“不能解析方法”变成(androi.view.View) 我想我做的一切都是错误的 –

+0

当然,你不能在TextView中添加图片,但你可以在ImageView。我编辑了我的答案。 – BenjaminBihr

+0

我尝试了新的但仍然没有工作,请检查我在主要问题中做了什么。 –