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
//...
}
Related
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.
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);
Android will allocate ids for each pics in the res/drawable directory. Where are they?
I want to dynamically choose one pic form the pool and show it. How can I do that?
They are stored in the res/drawable folder.
If the file name is demo.png, then they can be accessed by R.drawable.demo
If you want to access a random drawable, store all the resource identifiers in a Integer ArrayList, and programatically generate a random function using Random(), and get that particular item from the arraylist. Then you'll have a random drawable every time.
Autogenerated ids are in the gen file, but not advisable to use them. It would be better to use the filenames directly through some predefined array of R.drawable.filename and randomly pick them.
There IDs are stored in the R.java file, but you cannot edit it, as your changes are over written each time.
You can also access resources by name, which may be a viable approach to solving your problem if you know the names of the resources or can derive them according to some pre-defined naming scheme. (for example images are named in the sequence image1, image2 and so on.
You have to map the name to the identifier using the getIdentifier() method of the Resources class.
String name = "resource" + rng.nextInt(count);
int resource = getResources().getIdentifier(name, "drawable", "com.package");
The documentation for this method says:
Note: use of this function is
discouraged. It is much more efficient
to retrieve resources by identifier
than by name.
This is true but need not be a problem if you are doing it in code that isn't performance sensitive.
Alternatively, if you don't mind listing the resources in XML, you could create a typed array that you can then randomly select from.
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 want to load bitmaps into ImageViews in Android, but I don't want to use the R.drawable syntax because I have a lot of images with a standard naming convention so it's much easier to get the image with some logic. For example, all my images are named:
img1.png
img2.png
img3.png
So if a user selects a value, let's say x, I show "img" + x + ".png" in the ImageView. Looks like Bitmap.decodeFile is what I need, but I need to know the syntax of how to get to my drawables folder since that's where the images are. Or if there's perhaps a better way.
I realize I could do this with a switch statement instead of concatenating the image name but that would be a lot of lines since I have so many images.
One alternative is to use reflection to load images from R. You can looking static variables by string name. Keep in mind that reflection can be slow though. If you are going to be loading the same image multiple times, you might want to keep a cache of name -> R id.
Edit:
You can also access them by URI. See referring to android resources using uris.
The R.drawable value is a number by itself. You just need to use the first image id and add a fixed value to find the right image. For example:
R.drawable.image1 = 1234;
R.drawable.image2 = 1235;
etc.
So to get image 2 I would to R.drawable.image1 + 1.
Every time the R.java file is regenerated the numbers may change but the sequence will be the same. If you don't want to depend in this then you will have to look at the "assets" folder.
Looks like I've found a solution. The real problem is that I generate the resource names at runtime, but I can get the resource id like this:
getResources().getIdentifier("string1" + string2, "id", "com.company.package")
I can then pass that value to setImageResource or any other method that accepts a resource id.