2014-02-15 108 views
0

我想從資源設置BitMap,但圖像的名稱來自一個字符串。 我該如何設置它。我現在的代碼是這樣的。從資源中顯示圖像名稱中的圖像

    String Nombre= results.getString(0); 
       RelativeLayout oneLayout = (RelativeLayout)findViewById(R.id.relative1); 
       ImageView imageView = new ImageView(getApplicationContext()); 
       Bitmap bMap = BitmapFactory.decodeFile("res/drawable/"+Nombre); 
       Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.Nombre); 
       imageView.setImageBitmap(bMap); 
       oneLayout.addView(imageView); 

此行不工作,我知道:

Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.Nombre); 

我需要一個形式去做,任何幫助是惠康

回答

0

如果我理解你的問題,你的名字圖像,並希望獲得渣油和加載圖像?

假設你有繪製對象與文件名的圖像:

myimage.jpg 

試試這個片斷:

String nameOfImage = "myimage"; 
int resId = context.getResources().getIdentifier(name, "drawable", context.getPackageName()); 
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), resId); 
+0

它說: 獲取資源編號0x00000000時沒有包標識符 – user3314345

+0

您還在做:位圖bMap = BitmapFactory.decodeFile(「res/drawable /」+ Nombre);'?跳過並使用'位圖bitmap2 = BitmapFactory.decodeResource(getResources(),resId)''代碼。另外檢查你是否真的在可繪製文件夾中有你的圖像。如果您知道它在那裏,請嘗試清理該項目並重試。看看[http://stackoverflow.com/questions/5254100/how-to-set-an-imageviews-image-from-a-string?rq=1]替代。 – VonSchnauzer

+0

它說與其他解決方案相同...看起來這是代碼,並且該圖像存在於文件夾中:/ String Nombre = results.getString(0); \t \t \t \t Resources res = getResources(); \t \t \t \t int resID = res.getIdentifier(Nombre,「drawable」,getPackageName()); \t \t \t \t Drawable drawable = res.getDrawable(resID); 我該如何清潔這個專業?感謝幫助朋友。 – user3314345

0

問題,爲什麼「位圖BMAP = BitmapFactory.decodeResource(getResources() ,R.drawable.Nombre)「不工作是因爲名稱」Nombre「中有一個大寫字母,這是不允許的。刪除它,清理您的項目,然後運行它

+0

我知道,那不是問題。對不起,我的英語不好,但如果可以的話,請閱讀其他答覆,你會明白,謝謝你的時間:) – user3314345