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);
Related
Hi have an anroid application and I want take the ImageView from layout and set the image But I got the error: android.content.res.Resources$NotFoundException: Resource ID #0x0
I try log and It will show the uri,and imageResource is also 0
ps: the value of Log for building.getImage() is church however, I have church.png in my drawable folder
I will appreciate if you could help
Here is the code:
/* Extract the Building's object to show */
Buildings building = (Buildings) getItem(position);
ImageView imageCity = (ImageView) convertView.findViewById(R.id.ImageCity);
String uri = "drawable/" + building.getImage();
int imageResource = context.getResources().getIdentifier(building.getImage(), "drawable", context.getPackageName());
Log.e("URI", uri);
Log.e("context.getPackag", context.getPackageName());
Log.e("imageResource", String.valueOf(imageResource));
Drawable image = ContextCompat.getDrawable(this.context, imageResource);
imageCity.setImageDrawable(image);
the first parameter of getIdentifier is the name of the resource, whose id you are looking for, without extension. In your case this is building.getImage() and not uri, which is the concatenation of "drawable/" and the image's name. The fact the you want to look for it inside drawable, is specified in the second parameter of getIdentifier. Change from
int imageResource = context.getResources().getIdentifier(uri, "drawable", context.getPackageName());
to
int imageResource = context.getResources().getIdentifier(building.getImage(), "drawable", context.getPackageName());
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);
I have code like tthis:
String s = "replace__menu__" + data.imageid + ".png";
int RID = this.getApplicationContext().getResources().getIdentifier(s, "drawable-hdpi", getPackageName());
The String s = instruction sets a value that is the same as one of names in my res/drawable-hdpi folder. However, the returned value sets RID to the value 0
Any idea why my code is not working? Am I doing something wrong?
Try this
String s = "replace__menu__" + data.imageid; // image name is needed without extention
int RID = this.getApplicationContext().getResources().getIdentifier(s, "drawable", getPackageName());
".png" is not part of a ressource name
"drawable-hdpi" I would try just 'drawable' instead
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.
I have:
String uri = "#drawable/myresource.png";
How can I load that in ImageView? this.setImageDrawable?
If you really need to work with a string, try something like this:
private void showImage() {
String uri = "drawable/icon";
// int imageResource = R.drawable.icon;
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
Drawable image = getResources().getDrawable(imageResource);
imageView.setImageDrawable(image);
}
Else I would recommend you to work with R.* references like this:
int imageResource = R.drawable.icon;
Drawable image = getResources().getDrawable(imageResource);
First, don't do that, as that #drawable syntax is meaningless in Java code. Use int resourceId=R.drawable.myresource.
If for some reason you do wind up a resource name and need the integer ID, use getIdentifier() on the Resources object.
you can also add your own variable.. in my case scene.name between i followed #pfleidi answers. c stands for context.
String uri = "drawable/" + scene.name;
int imageResource = c.getResources().getIdentifier(uri, null, c.getPackageName());
imageList.setImageResource(imageResource);
Long since overdue, but my favorite is to use the Context:
context.getApplicationContext().getResources().getDrawable(R.drawable.placeholder_image)
where placeholder_image is the id of the resource. In your case, R.drawable.myresource.
Context can be an activity or the application. Pretty much wherever you are has reference to the context, which you can use to get the application's context.
In case anyone else comes across the problem I had, I was loading the image name from JSON and then needed to get the identifier. The second parameter of getIdentifier can be set to "drawable". This is a slight variation on #alia-ramli-ramli answer...
String uri = details.getString("circle").toLowerCase();
int imageResource = context.getResources().getIdentifier(uri, "drawable", context.getPackageName());
Drawable image = context.getResources().getDrawable(imageResource);
viewHolder.unit_circle.setImageDrawable(image);
You can check here
int resID = mContext.getResources().getIdentifier(stringfilename, "drawable", mContext.getPackageName());
iv_imageview.setImageDrawable(ContextCompat.getDrawable(mContext,resID));
you can also retrieve image from mipmap by replacing "drawable" with "mipmap" parameter of getIdentifier() method.
I had the same problem with ... the deprecated etc. etc.
I solved in such a way with minimum API 14 okay.
...
...
ImageView imgPoster = viewCache.getImgPoster(resource);
String uri = "drawable/" + film.getPoster();
int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
Drawable image = ContextCompat.getDrawable(context, imageResource);
imgPoster.setImageDrawable(image);
return convertView;
...
...