2013-03-18 29 views
0

我想通过我自己的属性检索绘图文件夹中的drawable,而不是使用R.drawablegetDrawable从资源

我该如何实现这一目标?

public void updateLanguageImg(String img) 
{ 


     Bitmap myBitmap = BitmapFactory.decodeResource(this.getResources(),R.drawable.); 
       // i want retrieve the img String here 
     ImageView imageV = (ImageView) findViewById(R.id.imageLangue); 
     imageV.setImageBitmap(myBitmap); 

} 

非常感谢您

回答

1

如果我正确理解的问题,并根据该法收到的参数,要通过字符串名调用的资源,因此,如果这是正确的,这是什么你要做的:

“(bitmapName ” “文件夹名”,this.class.getPackageName())“

其中context.getResources()则getIdentifier”:文件夹名称可能是( “原始”,“ drawable“等...)

这将返回identifierNumber这是你所拥有的已经,并且可以在代替使用的方法,这样的事情:

INT ID = context.getResources()则getIdentifier(“bitmapName”,“文件夹名” ,this.class.getPackageName();

位图myBitmap = BitmapFactory.decodeResource(this.getResources(),id);

问候!

+0

如果资源未找到,您还需要故障转移 – nicopico 2013-03-18 23:56:26

+0

@Martin Cazares非常感谢。它像一个魅力 – user2177823 2013-03-19 20:30:13

+0

没问题,很高兴帮助... – 2013-06-04 20:31:53