Use dynamic filename in Android R.drawable.<filename> - android

I'm trying to dynamically construct the filename of an image in Android to use in the R.drawable. clause.
Here's my code:
imgHouse = (ImageView)findViewById(R.id.imageView);
strHouseNameImageFile = strHouseName + ".png";
imgHouse.setBackgroundResource(R.drawable.<filename>);
And in the last line above, I really want it to be a combination of the house name and .png but I'm not sure how to put a string together that will be used by R.drawable.
Could I do something like this for the last line, which now gives an error in Android Studio:
imgHouse.setBackgroundResource(strHouseNameImageFiles);
Any help would be appreciated.
Thanks

int iconResId = getResources().getIdentifier(strHouseName, "drawable",getPackageName());
imgHouse.setImageResource(iconResId);

The way you are trying to set the background resource may not work since the R class is generated at compile time. The R.drawable.*** is really an integer which is generated at compile time. The closest match to your problem may be this post : Dynamic loading of images R.Drawable using variable

Related

Using string from Cursor to get drawable to ImageView

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.

how to include images into an android project best possible manner?

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

Is it possible to add images programmatically?

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 .

How to convert a string into a path

How to convert a string like "R.drawable.online" into a path, which can represent an image?
What should I do for this?
Take a look at this answer:
Android and getting a view with id cast as a string
By the way... that kind of resources cannot be access using a path... you must use the integer representation of it.
if you have an image inside your folder /drawable/online.png
you will load your image for example into a ImageView...
ImageView.setImageDrawable(getResources().getDrawable(R.drawable.online));

Android Bitmap syntax

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.

Categories

Resources