2013-03-18 28 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