2014-03-04 159 views
0

我在我的android應用程序的可繪製文件夾中有170個圖像。我有一個活動顯示所有這些。想要做的是將點擊的圖像視圖傳遞給另一個活動(Zoom_activity),用戶可以在其中放大並使用它。我如何實現它?將ImageView從一個活動傳遞到另一個活動 - 意圖 - Android

所有的圖像都是500x500px。所以我想不出將它們解碼爲Bitmaps並通過Intent傳遞Btmaps。請建議一個更好和簡單的方法來做到這一點!我已經看過這裏的其他答案,但他們都沒有解決我的問題。

這裏是我的代碼:

Activity_1.java

Intent startzoomactivity = new Intent(Activity_one.this, Zoom_Image.class); 
String img_name = name.getText().toString().toLowerCase(); //name is a textview which is in refrence to the imageview. 
startzoomactivity.putExtra("getimage", img_name); 
startActivity(startzoomactivity); 

Zoom_Activity.java

Intent startzoomactivity = getIntent(); 
    String img_res = getIntent().getStringExtra("getimage"); 
    String img_fin = "R.drawable."+img_res; 
    img.setImageResource(Integer.parseInt(img_fin)); 

錯誤:應用強行關閉

請幫我解決這個問題!
謝謝!

回答

1

Integer.parseInt()僅適用於字符串,如「1」或者「123」確實只包含Integer的字符串表示。

你需要的是通過它的名字找到一個可繪製的資源。

這是可以做到使用反射:

String name = "image_0"; 
final Field field = R.drawable.getField(name); 
int id = field.getInt(null); 
Drawable drawable = getResources().getDrawable(id); 

或者使用Resources.getIdentifier()

String name = "image_0"; 
int id = getResources().getIdentifier(name, "drawable", getPackageName()); 
Drawable drawable = getResources().getDrawable(id); 
0

你正在嘗試的是錯誤的。您無法將"R.drawable.name"Integer.parseInt進行轉換。 Integer.parseInt期待類似"100"。您應該使用

getIdentifier(img_fin, "drawable", getPackageName()); 

檢索資源ID您正在尋找

0

使用getResources().getIdentifier從繪製對象加載圖像中的ImageView爲:

int img_id = getResources().getIdentifier(img_res, "drawable", getPackageName()); 
img.setImageResource(img_id); 
相關問題