开发者

Load Images in ImageView in Android

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

so on button clicking, i want to lo开发者_Go百科ad 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."


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.


Why not something like

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


I implemented below solution and it's working for me:

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


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


It works for me:

Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.my_image)
.copy(Bitmap.Config.ARGB_8888, true);
Canvas c = new Canvas(myBitmap);
// etc.  ....... 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜