Pass ImageView from one activity to another - Intent - Android - android

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!

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

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

Use getResources().getIdentifier to load image from Drawable in ImageView as:
int img_id = getResources().getIdentifier(img_res, "drawable", getPackageName());
img.setImageResource(img_id);

Related

How to set customize imageView ID?

I have several image-views in my xml file, i need to initialize and use them dynamically.
In the image currentParam.x and y are coordinates.
Is there any way to do this task.
To get a resource id out of the resource name, use this code below. i tested in my project. working like a charm.
String packageName = getPackageName();
String tempCurrent = "square" + String.valueOf(currentParams.x+1) + String.valueOf(currentPrams.y+1);
int resId = getResources().getIdentifier("square", "id", packageName);
currentImageView = (ImageView) findViewById(resId);

How to select a drawable image by matching a string value to the image name?

I have an arraylist of strings, I need to randomly select an index and if the string value was
"bear", then set background of button to bear.jpg.
OK, my research shows that resources are accessed by an int id, not their name, I am not sure the best way to achieve what I want to do. Here is my code:
list.add("alligator");
list.add("bear");
list.add("beaver");
list.add("bison");
randomInt = randomGenerator.nextInt(list.size());
b1.setBackgroundResource(R.drawable.list.get(randomInt));
Now of course the last line of the code is wrong, I wrote it to show what I want to achieve. My latest attempt to accomplish this was trying to get the resource id and access the resource this way, but I don't know if this is the way to do this, and if it is I am not using the correct code. I am trying hard to do this by myself but I could use some advice on what to do here. Here is my attempt:
String mDrawableName = "bear";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
String s= Integer.toString(resID);
Use the below code
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
b1.setBackgroundResource(resID);
Try this:
list.add("alligator");
list.add("bear");
list.add("beaver");
list.add("bison");
randomInt = randomGenerator.nextInt(list.size());
int resourceId = getResources().getIdentifier(list.get(randomInt), "drawable", getPackageName());
b1.setBackgroundResource(resourceId);

Creating Dynamic R.drawable source and ImageView

I have an Activity which has variable position that comes from another activity. I handle this variable with Intent like below. (Actually position is listView's position from another activity.) and values of position variable are 0 to 200
I took my Extra, then assigned it to a string than I parsed it to an Integer. These are normal and easy for me and working.
Intent i = getIntent();
String position = i.getStringExtra("POSITION");
int position_to_int = Integer.parseInt(position);
But my problem is about resources.
Simply I want to put an image to my LinearLayout dynamically . I have some country flags and they named in format like 0.gif, 1.gif, 2.gif, 3.gif ...
My idea is using position variable's value for this purpose.
but when I try to use position_to_int variable for name of flag, Eclipse returning error normally. Because R.drawble.XXX values storing in R.java
ImageView imageView = new ImageView(this);
// setting image resource
imageView.setImageResource(R.drawable.position_to_int);
How can I use position_to_int variable to show an ImageView dynamically?
You can't access it directly. You'll have to get the resource using its name:
private Drawable getDrawableResourceByName(int name) {
String packageName = getPackageName();
int resId = getResources().getIdentifier("character_you_prefixed" + String.valueOf(name), "drawable", packageName);
return getResources().getDrawable(resId);
}
Note that your resource names must begin with a letter, not a number, or else your project will not compile. Try adding a character to the beginning of each file name.

Android, reference things in R.drawable. using variables?

say I want to dynamically load an image file in R.drawable.* based on the value of a string
Is there a way to do this? It seems like I need to statically refer to anything in R.
Have you declared the id for the image in XML file? If you did, you can use the following method:
Let's say you have picture.png in your res/drawable folder.
In your activity, you can set your image resource in the main.xml file
<ImageView android:id="#+id/imageId" android:src="#drawable/picture"></ImageView>
In FirstActivity
//to retrieve image using id set in xml.
String imageString = "imageId"
int resID = getResources().getIdentifier(imageString , "id", "package.name");
ImageView image = (ImageView) findViewById(resID);
imageString is the dynamic name. After which you can get the identifier of the dynamic resource.
Another method, you can do this:
//to retrieve image in res/drawable and set image in ImageView
String imageName = "picture"
int resID = getResources().getIdentifier(imageName, "drawable", "package.name");
ImageView image;
image.setImageResource(resID );
You will be able to reference your image resource and set your ImageView to it.
int drawableId = getResources().getIdentifier(drawablename, "drawable", getPackageName());
imageview.setImageResource(drawableId);
Try this. This should work.
Class res = R.string.class;
Field field = res.getField("x" + pos);
headerId = field.getInt(null);
header.setText(headerId);
this works with drawables as well, just edit the string. the header part is not required, it's just an example pulled from something I wrote a while ago.

dynamic image source android

I am trying to make images dynamic in my android application. With the help of other posts i wrote this code but i am unable to set image resource. Here is my code.
// get image whose source i want to change..
ImageView IV = (ImageView) findViewById(R.id.imageView1);
// x for 1,2,3.. hangman1.png,hangman2.png and so on image are located
// under res/drawable-mdpi
int j = getResources().getIdentifier("hangman"+x, "imageView", getPackageName());
// here i get errork, The method setImageResource(int) in the type ImageView
// is not applicable for the arguments (Drawable)
IV.setImageResource( getResources().getDrawable(j) );
int j = getResources().getIdentifier("hangman"+x, "drawable", getPackageName());
try this, if you are trying to get a drawable. and also as kcoppock said , just use j.
Because you're trying to pass in a Drawable. getDrawable(), as its name states, returns a Drawable object. setImageResource takes a resource ID, which is what getIdentifier will return. You should just pass j to setImageResource().
Edit: Heh, and you also should use Yashwanth's answer, as he said. :)
(TEAMWORK!)
combining both above solutions did the trick for me..here is code..
dnt know which answer i should accept..
ImageView IV = (ImageView) findViewById(R.id.imageView1);
int j = getResources().getIdentifier("hangman"+x, "drawable", getPackageName());
IV.setImageResource( j );
from your xml file , use app:src instead of android:background, and from the activity file, use :
setImageDrawable(getRessource().getDrawable(R.drawable.yourRessource));

Categories

Resources