Reputation: 5140
I have 170 images in the drawable folder in my android app. I have one activity displaying all of them. What is want to do is to pass the clicked imageview to another activity (Zoom_activity) where the user can zoom it and play around with it. How do I achieve it?
All the images are 500x500px. So I can't think of decoding them into Bitmaps and passing Btmaps via Intent. Please suggest a better and simple way to do it! I have already had a look at the other answers here on SO but none of them solved my problem.
Here is my code:
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));
Error: App force closes
Please help me solve this problem!
Thanks!
Upvotes: 0
Views: 1679
Reputation: 12919
Integer.parseInt()
only works for strings like "1" or "123" that really contain just the string representation of an Integer.
What you need is find a drawable resource by its name.
This can be done using reflection:
String name = "image_0";
final Field field = R.drawable.getField(name);
int id = field.getInt(null);
Drawable drawable = getResources().getDrawable(id);
Or using Resources.getIdentifier()
:
String name = "image_0";
int id = getResources().getIdentifier(name, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);
Upvotes: 1
Reputation: 132992
Use getResources().getIdentifier
to load image from Drawable in ImageView as:
int img_id = getResources().getIdentifier(img_res, "drawable", getPackageName());
img.setImageResource(img_id);
Upvotes: 0
Reputation: 157487
What you are trying is wrong. You can not convert "R.drawable.name"
with Integer.parseInt
. Integer.parseInt is expecting something like "100"
. You should use
getIdentifier(img_fin, "drawable", getPackageName());
to retrieve the resources id you are looking for
Upvotes: 0