Uploading images to ImageView on Android

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

so that clicking a button on a button, I want to load an image one by one in ImageView .... I want to do like:

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

so my confusion is that "it is all the same to load an image in an image representation from a string (imagename variable), for example, temp1.jpg, etc."

+5
source share
4 answers

I have implemented the solution below and it works for me:

while(cnt!=n)
{
 String icon="temp" + cnt;
 int resID =
 getResources().getIdentifier(icon,"drawable","testing.Image_Demo");
 imageView.setImageResource(resID);
 cnt++; 
}
+3
source

:

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++;

, .

+4

-

File f = new File(PathToFiles + "/temp" + cnt + ".jpg");
if (f.exists()) {
  Drawable d = Drawable.createFromPath(f);
  imageview.setImageDrawable(d);
}
+2

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

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

and then you can load the ImageView from drawable.

 String imageName = "temp" + n;
 Drawable d = getResources().getDrawable((int)map[imageName]);
 ImageView i = new ImageView(this);
 i.setImageResource(d);
+1
source

All Articles