Android - Open resource from #drawable String - android

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

Related

android call resource file by his name

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

Got error when I want to take the ImageView from layout and set the image

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

How do a load a specific drawable by using a string/int?

If I have a variable with the number associated with a gridview position (id int 14) how do I make the code below load the correct drawable? (ie hccat14)
mBitmap = getImageFromResource(getContext(), R.drawable.hccat14,w, h);
Thanks,
Shannon
I don't know exactly how you are trying to use it, but it should be something like this.
String resouceName = "hccat" + Integer.toString(14);
int resourceID = getResources().getIdentifier(resourceName,
"drawable", getPackageName());
Here's what I use, just set the stringName on click
imageResource = Classname.this.getResources().getIdentifier(stringName,
null, null);
mBitmap.setImageResource(imageResource);
EDIT I store the image name as packagename:drawable/imagename in my database

How to Dynamically change drawable's id in setImageResource() Android

My Question is how to change image id from drawable folder in setImageResource() in android.
My drawable folder contains icon0.png to icon9.png and i want to change these images in image view dynamically using this
ImageView iV3;
iV3 = (ImageView) findViewById(R.id.imageView3);
iV3.setImageResource(R.drawable.icon + speed_Arr[2]);
speed_Arr[2] contains any value from 0 - 9.
But this didnt change images.
Plz help me out.
regards.
public static int getIdentifier(Context context, String name)
{
return context.getResources().getIdentifier(name.substring(0, name.lastIndexOf(".")), "drawable", context.getPackageName());
}
Above code will return the resource id from name String.
int res = getResources().getIdentifier("< packageName:drawable/imageName >'", null, null);
Use this res in your iV3.

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.

Categories

Resources