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()));
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 database with string names and string filenames that I am using to dynamically create a page. On the previous page based on the button selected the cursor returns the specific entry needed, and then that is used to find and display the corresponding image. I've been trying to mish-mash code from different places, but nothing seems to be working. It currently looks like this:
ImageView iv = (ImageView) findViewById(R.id.iv1);
Bitmap bmap = (Bitmap) BitmapFactory.decodeResource(this.getResources(),
getResources().getIdentifier(c.getString(4), "drawable", getPackageName()));
iv.setImageBitmap(bmap);
For debugging purposes I've had the cursor getString(4) toasted and it comes out correctly as "al_0" or what it should be, and there is a file in the "drawable" folder in res named "al_0.jpg".
I've tried various combinations of concatenating the packagename and drawable folder location directly with the image name, and putting null in the other variable positions. Each time either I get a crash with a reference to the decodeResource line or that the "No package identifier when getting value for resource number 0x00000000."
Any help would be appreciated, and if more specifics are needed I can try to provide them.
Update: So it seems like something else is wrong, using even the following I get a crash:
ImageView iv = (ImageView) findViewById(R.id.iv1);
iv.setImageResource(R.drawable.al_0);
and R.drawable.al_0 is definitely a correct resource in drawable-mdpi (I moved it by a suggestion from Lint).
Here is what I do.
Store the filename in the format com.example.appname:drawable/filename Notice the colon between appname and drawable.
Then after I pull that string from the database I use this, where imageString is the string from the cursor.
imageResource = ClassName.this.getResources().getIdentifier(imageString, null, null);
imageView.setImageResource(imageResource);
So it turns out everything was completely fine, my images were just to large. Using BitmapFactory.Options.inSampleSize I was able to get everything working fine. Thanks for the helpful debugging tips, I apologize for the mishap.
Update: I also needed to bmap.recycle() as these views were being dynamically created, and they were causing memory errors after a few were opened. Both worked out as being optimal solutions to the problem.
My android app is basically a simple one where there is one listview with 40 list items. And each of those items when clicked leads one to an image. so there are 40 images.
I can always of-course place those images in the res/drawable folder and refer to them directly from my java file in which case there will be hardcoding done
img.setImageResource(R.drawable.day1);
.
.
.
img.setImageResource(R.drawable.day3);
.
.
.
etc.
Is there anyway (by storing the names of the images in an xml for example) to programatically retrieve the image names from folder and display them ?
int resID = getResources().getIdentifier("day" + i,
"id", getPackageName());
where i is your index
img.setImageResource(resID);
Create dynamic bitmap object using resource.
ImageView image = (ImageView) findViewById(R.id.test_image);
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/"+"test"+index+".png");
image.setImageBitmap(bMap);
Used same name of image like, image1,image2...
call using for loop or index..
What I would do:
I'd place the images in the res/drawable folder as you said, but wouldn't call them like you said; too much work! Try creating an array "Images", filled with a for loop where you add on each position something like:
for (i;i<x;i++) //Where x is the number of images you use
Images[i] = "R.drawable.day"+String.valueOf(i);
// Even you could try with:
// Images[i]="img.setImageResource(R.drawable.day"+String.valueOf(i)+")";
Like this, you just have to call images[i] to call image "i"
I hope this helps you :)
A simple manner to do this is like this:
You place your images in res/drawable, name all using a certain pattern (for instance: img1, img2, img3 ... ) and after that you load them dynamically using a pattern for names like this:
String imageName =IMAGE_PATTERN+id;
int imageResource = getResources().getIdentifier(imageName,null,getPackageName());
imageView.setImageDrawable(getResources().getDrawable(imageResource));
where
IMAGE_PATTERN may be something like "image" or "img" or whatever you used to name your files :)
good luck,
Arkde
I have a web service where I read various information from. One of these pieces of information is the name of an image. I have all the images stored locally in the drawables folder. What I have done right now is I read the string of the image I need from the web service. I want to use this string name and insert the image with the same string name from my drawable folder. Is this possible to do?
Is it possible to do the following somehow?
//Setup the image
ImageView spotIvIcon = (ImageView)findViewById(R.id.spot_selected_image);
String temp = "R.drawable."+spotImage;
spotIvIcon.setImageResource(R.drawable.beer);
What I do with the temp string. Is it possible to convert this string into the int I need? I have the beer in there just to test something out.
Thanks
So I found out how to do this. If anyone wanted to know how it's done use the following code:
int path = getResources().getIdentifier(spotImage, "drawable", "com.androidpeople.tab");
spotIvIcon.setImageResource(path);
Where:
spotImage is the name of your string
drawable is the type you want
com.androidpeople.tab is your package name
Works like a charm now.
you could just use the assets directory to store these images, and then load them using something like BitmapFactory.decodeStream(assetManager.open(imAnAssetName));
the asset directory it's here to be used for things you don't want to be managed (or that can't be managed) by the resource manager.
http://developer.android.com/reference/android/content/res/AssetManager.html .