Android Bitmap syntax - android

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.

Related

count how many pictures in drawable directory android project

I need help to count how many pictures in drawable with criteria substring filename? like how many pictures with filename starting "SC" ?
You can use reflection for that. Simply use getDeclaredFields() to get all fields of R.drawable.class. Then, just loop through them, look for the search term in the field names and get the drawable using Resources.getDrawable(Field.getInt());.
Well,
I haven't tried it but you can use getFields() to get all the variables
R.drawable.class.getFields()
Then you can perform your search for specific file name accordingly.
See this to know more for Reflections in Java: http://www.ibm.com/developerworks/library/j-dyn0603/

Get resources by int number

I sometimes see this declarations in Android source code:
mContext.getString(2131361954);
Notification n = new Notification(2130837696, "123", System.currentTimeMillis());
// Example code - does not match together
I think the numbers are some resources from the project, right? Why sometimes people work with this numbers instead of using the R class? Is it faster or something else?
And how can I check which resource is assigned to that numbers? Is it possible to get number which is used if I only have the file or is this number random? Maybe with the file name or the MD5 hash of the file or something else?
There is no performance difference, as all members of the R class are static and final, and are directly swapped in during compile time. This is equivalent to any code that uses R.x.y, so the performance is the same.
I would strongly recommend against using the numbers directly in your project as they may change during the addition, removal and modification of resources.
You can check the resource to which that number corresponds by converting it to hex, opening up the R.java file and searching for that hex number and seeing what it is assigned to.
You can also use getResources().getResourceEntryName(int resid); and pass it the ID at runtime to retrieve the file name.

Where are the resources' IDs?

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.

How to add R.drawable resources to an array for android?

I would like to have sequence of set R.drawable been called in integer array so that it can be accessed later with choice.But I'll like to load to the R.drawable dynamically which corresponds to different names.It names needs to be had as per the external value input not hardcoded.I tried this in making many but like use list,set,array conversion etc.Kindly guide me with a snippet or example on this regard.Thank you.
I suppose, that best way to achieve this funcionality is by using ArrayAdapter of Integer.
You will simply add drawble resources by id.
If you need to get resource id, but you will get string input from user, you can use this code:
Android, getting resource ID from string?

android Image problem

Actually I have 52 images which r basically cards.
images names are from 1 to 52.
when I put all the 52 images in my drawable folder then it is showing an error in R.java file which is:
Syntax error on token "image name(any
between between 1 to 52)", invalid
VariableDeclaratorId
what is the problem?
thanks for replying.
i think u didnt get my problem.
i have given name to my cards from 1 to 52 because i need to randomly select one card from it.
Resource names have to be proper Java identifiers. Call them card1 through card52 instead of just their numbers (if I understand you correctly).
EDITED TO ADD: To map an integer to the correct image, your code should manage the mapping itself. One (not terribly elegant) way is to explicitly create a Bitmap[] cardImages = new Bitmap[52]; array and assigning each resource into the array, as in e.g.
Resources r = context.getResources();
cardImages[0] = loadBitmap(r.getDrawable(R.drawable.card1));
// ...
cardImages[12] = loadBitmap(r.getDrawable(R.drawable.card13));
// ...
cardImages[51] = loadBitmap(r.getDrawable(R.drawable.card52));
The problem is that Android doesn't allow to use spaces in a file identifier
Pontus Gagge is right. Android will take the name of the everything in the drawable folder and will try to generate an R file that contains an int for every image that you are using in your app. The ints are named after the file names of your drawables. You can then later use this ints as ids to load the images from your app.
The problem is that Java does not allow a vairable name to start with a number. Your images start with a number therefore your variables in the R file will start with a number. You have to choose another name for your images.

Categories

Resources