Hello I'm a new to android and I'm trying to display a photo base on a id (number between 1 and 52) I have in my resource folder pictures of 52 card with a name of ic_1 to ic_52
and i need to be able to create bitmap from them base on a id i tried do this (card.getId() give the id number between 1 and 52)
String cardView = "R.drawable.ic_"+card.getId();
Bitmap bm = BitmapFactory.decodeResource(getResources(), Integer.parseInt(cardView));
but it didn't worked any help would be welcome thanks.
you can get the ID this way:
int resourceID = getResources().getIdentifier("ic_"+card.getId(), "drawable", getPackageName());
You can use getIdentifier()
int id = getResources().getIdentifier("ic_" +card.getId(), "drawable", getPackageName());
Bitmap bm = BitmapFactory.decodeResource(getResources(), id);
to get the int value of your resource you need to use getIdentifier method.
String mDrawableName = "R.drawable.ic_"+card.getId();
int resID = getResources().
getIdentifier(mDrawableName , "drawable", getPackageName());
Related
I cant load an image from #drawable to ImageView using String as name of the image.
Here's my code;
String uri = "#drawable/"+imgname; // image file
int resID = getResources().getIdentifier(uri , "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(resID); //<----line with error
ImageView imageview= (ImageView)findViewById(R.id.hardImg);
imageview.setImageDrawable(drawable);
error log:
android.content.res.Resources$NotFoundException: Resource ID #0x0
You don't need "#drawable/". That is what the second parameter to getIdentifier already does.
getResources().getIdentifier(imgname, "drawable", getPackageName());
Will generate the integer resource ID for <your package name>.R.drawable.<imgname>, or 0 if that resource does not exist.
Returns 0 if no such resource was found. (0 is not a valid resource ID.)
Thus, your error message.
Note : Android resources can only contain certain characters.
This should work -
String uri = "#drawable/"+imgname; // image file with extension, make sure image name is correct.
int resID = getResources().getIdentifier(uri , null, getPackageName());
Drawable drawable = getResources().getDrawable(resID);
ImageView imageview= (ImageView)findViewById(R.id.hardImg);
imageview.setImageDrawable(drawable);
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 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);
int igotit = getResources().getIdentifier(val, "drawable", getPackageName());
i got resourceid from that how to display imagename?
use this :
String imageName = getResources().getResourceName(R.id.img);
refer doc . there is also the method : getResourceEntryName(int)
To use the method
getResources().getIdentifier(val, "drawable", getPackageName());
the val is the image name
check here
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;
...
...