I am trying to get Id of image resource in drawable folder.
Here is my code.
...
int resourceId = Context.Resources.GetIdentifier("Elements_ok", "drawable", this.context.PackageName);
I always got the result of Zero.
But I got Id of image named error.png.
int resourceId = Context.Resources.GetIdentifier("error", "drawable", this.context.PackageName);
Is there a way that I can fix it?
Yep, just change your drawable filenames to lowercase.
And, of course, update any code that uses those filenames accordingly to use lowercase strings.
Since Android only supports lowercase filenames for resource items, I would also recommend updating all your drawable resource filenames to lowercase to eliminate any other potential issues.
Related
I have a lot of images (more than 200) with various sizes in mipmap folders, and I need to set images in ImageView dynamically... but how to do this without "direct description" in my code of that R.mipmap.*int files?
There are flag images in mipmap folders, and my object has it's own String field "flagCountry" which consists of two characters. Names of flags in mipmap folders consist of same characters. So, that's a trouble - how to dynamically set setImageResource(R.mipmap.XXX) from flagCountry String variable?
I'm thinking that decision in "reflection", but how is it possible? )) Please help
You can use Resources.getIdentifier() to find the integer id for a resource given its name. It's like reflection for android resources.
int id = getResources().getIdentifier("XXX", "drawable", getPackageName());
Or something along those lines. This assumes the code is in an activity. Then you can call setImageResource(id) with that id.
As said by Doug, you can use Resources.getIdentifier() to find the integer id for a resource given its name. But you said you wanted the resource from mipmap. So do a simple change. Change the word "drawable" to "mipmap", and you'll get the image from mipmap. The changed code:
int id = getBaseContext()getResources().getIdentifier("XXX", "mipmap", getBaseContext().getPackageName());
image.setImageResource(id);
I would like to load images from my Drawable Resource folders without explicitly typing the identifiers for them. For example : Loading the images in drawable-mdpi using a 'for' loop and using the integer as an argument for the Bitmap Decoder. Which seems to work in theory, but my concern is: is the integer 0x7F020000 where drawable Int ID's seem to begin constant across all devices? or if I create code to load images this way will it not work on a different Android device than the one I am testing on?
Is there a better way to achieve what I have described?
You should not hard-code the id even you can get it from R file. From android API:
Although the R class is where resource IDs are specified, you should never need to look there to discover a resource ID.
R class will change its content when you update your resource, and clean and rebuilt. It means you cannot make sure the same name of resource will be assigned the same value after each built. However, IDs will be the same (not change) after apk released and installed in different devices.
Simple solution:
Create your images and name them sequentially like: img0.png, img1.png, ... and place them in drawable folder.
Get image id from its name by:
int imgID = getResources().getIdentifier("img00" , "drawable", getPackageName());
So if you want to use for loop, it should be something like:
for(int i=0; i < total_image_number; i++) {
int imgID = getResources().getIdentifier("img"+i , "drawable", getPackageName());
//do something else with our imgID here
//...
}
I have a list of Drawable stored in a Array. Now I would like to convert it into "res"(int datatype). Is this possible? Any help is appreciated.
It is not possible to modify the resources in an existing APK on the device.
You're probably trying to get a resID from a random Drawable. If so then this is impossible - drawable doesn't store IDs.
But if you use TypedArray to load your drawables from resources (i.e. Resources.obtainTypedArray(R.array.your_imgs)) then you'll be able to get resID by calling getResourceId(int index, int defValue):
TypedArray typedArray = mResources.obtainTypedArray(R.array.my_imgs);
int resID = typedArray.getResourceId(index, defValue);
"res" is an identifier to access a resource packed in .apk file. So I think to "convert" drawable to res actually means modifing existing .apk on device, which is impossible
if you have the drawable names in the array you can use
getIdentifier (String name, String defType, String defPackage);
int resId = getResources().getIdentifier("us","drawable","com.app");
The above function will return an integer value same as R.drawable.us.
This is how you access with resource names.
I have an android application that use a gallery component to choose an icon and assign it to a button.
The icon set is located in res/drawable folder and is accessed in the gallery with the typical adapter of the guide:
private Integer[] Imgid = {
R.drawable.icon_home,
R.drawable.icon_home2,
...
}
After an icon choosing, i stored the settings in a db with id of the button and of the drawable.
All works done, but i've noticed that if i'll want to add or modify my icon set or resources in general of my application, ids of old resource could change, so the preferences in the db refers to wrong icon.
Is there a way to force the old id of R class so they didn't change in the future? Or is there another way to manage the item of the component galley with another attribute than id? (maybe string name and reflection?)
Thanks in advance
You can store the name of the drawable in the database if you don't plan to change that. getResourceEntryName returns the name from the resource id:
Resources resources = getResources();
String name = resources.getResourceEntryName(R.drawable.icon);
And you can get back the resource id from the name with getIdentifier:
int resId = resources.getIdentifier(name, "drawable", "your.package.name");
You can't use static int for resource identifier, however you should look at two methods od Resources class:
getIdentifier()
getresourceName()
You shouldn’t rely on the actual values of the R.drawable.* attributes.
Create your own ids instead (for example 1 correspond to R.drawable.icon_home and 2 correspond to R.drawable.icon_home2)
Edit:
String name and reflection should work too, but it’s probably a little overkill you have only a few icons.
I have this database of 100+ images (of country flags) in my drawable folder.
Now I want to display the flag of the country you're currently in, in an ImageView.
I get the country with String country_variable = address.getCountryCode();
And I set the image with flag.setImageDrawable(getResources().getDrawable(R.drawable.country_variable));
As you all know R.drawable.country_variable wont't work because the compiler can't find a image named country_variable in the drawable folder.
What is the best way to do this?
You should be able to use getResources().getIdentifier() to get the id by the resource name. Something like:
flag.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("drawable/" + country_variable, "drawable", getPackageName()));
Try this:
flag.setImageDrawable(getResources().getDrawable(getResources().getIdentifier(country_variable, "drawable", getPackageName()));