簡體   English   中英

將ImageView從一項活動傳遞到另一項活動-Intent-Android

[英]Pass ImageView from one activity to another - Intent - Android

我的Android應用程式中的drawable資料夾中有170張圖片。 我有一個活動顯示所有這些。 要做的是將單擊的圖像視圖傳遞到另一個活動(Zoom_activity),用戶可以在其中縮放它並進行操作。 我該如何實現?

所有圖片均為500x500px。 所以我想不起來將它們解碼為位圖並通過Intent傳遞Btmap。 請提出一種更好,更簡單的方法! 我已經在SO上查看了其他答案,但是沒有一個解決了我的問題。

這是我的代碼:

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));

錯誤:強制關閉

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

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);

您嘗試的是錯誤的。 您不能使用Integer.parseInt轉換"R.drawable.name" Integer.parseInt期望的值類似於"100" 你應該用

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

檢索您正在尋找的資源ID

使用getResources().getIdentifier可以從ImageView中的Drawable加載圖像,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM