views:

302

answers:

5

Hello all,

In my application....there are some images like temp1.jpg, temp2.jpg .....upto temp35.jpg,

so on button clicking, i want to load one-by-one image in ImageView .... i want to do like:

cnt=1;
imagename="temp" + cnt + ".jpg";
cnt++;

so my confusion is that "is there anyway to load an image in imageview from string(imagename variable) like temp1.jpg,etc."

A: 

I don't know if this is the best solution but you can make a Hashtable that maps the image names to the resources.

Hashtable map;
map.put("temp1", R.drawable.temp1) // assuming temp1.jpg is in /drawable

and then you could load the ImageView from a drawable.

 String imageName = "temp" + n;
 Drawable d = getResources().getDrawable((int)map[imageName]);
 ImageView i = new ImageView(this);
 i.setImageResource(d);
Itsik
That means you need to edit the hash table every time you add a new image...
xil3
Thats why I wrote its not the best solution. Although, you can use Reflections to read the public fields and load the hashmap automatically. Come to think of it, you can use Reflections to just get the id and forget about the map.
Itsik
+1  A: 

You could try this:

int cnt = 1;
//Bitmap bitmap = BitmapFactory.decodeFile("temp" + cnt + ".jpg");
int imageResource = getResources().getIdentifier("drawable/temp" + cnt + ".jpg", null, getPackageName());
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imageResource);
imageView.setImageBitmap(bitmap);
cnt++;

Hope that's what you were looking for.

xil3
Sorry..it is not working...imageview is displyed with blank screen only..not with image
PM - Paresh Mayani
Are you sure you're supplying the right path to the image? Are the images on the sdcard?
xil3
Can you also post your ImageView (the xml or if you declare it programatically)?
xil3
but thanx for the answer
PM - Paresh Mayani
how do i get path of my image file...my image is stored in /res/drawable folder..so may be i am giving wrong path..so pls let me know about my image's path..
PM - Paresh Mayani
In that case, I changed the code a bit - check it out.
xil3
then why are you using decodeResource() method....that i didnt get,and you can directly set the image in imageview as: imageView.setImageResource(imageResource);
PM - Paresh Mayani
Yea, you can directly set the image as a resource, but if you decode it as a bitmap, it gives you more power. If later on you wanted to scale the image (if it's too big), you could do that with the raw bitmap before passing it to the imageView. That's just an option though - if you never have any need to modify the image (for any reason), then passing it directly works just fine.
xil3
+1  A: 

Why not something like

File f = new File(PathToFiles + "/temp" + cnt + ".jpg");
if (f.exists()) {
  Drawable d = Drawable.createFromPath(f);
  imageview.setImageDrawable(d);
}
slup
thanx for the answer....
PM - Paresh Mayani
+1  A: 

Finally this is working for me....

while(cnt!=n)
{ String icon="temp" + cnt;

int resID = getResources().getIdentifier(icon, "drawable", "testing.Image_Demo"); imageView.setImageResource(resID);
cnt++; }

However...thanx all for your support......

PM - Paresh Mayani
A: 

no it is not reply right answer.

nenshi