2010-07-07 107 views
5

在我的应用程序中....有一些图像像temp1.jpg,temp2.​​jpg ..... upto temp35.jpg,在Android ImageView中加载图片

等等按钮点击,我想负载一个接一个的图像中的ImageView .... 我想要做的,如:

CNT = 1;
imagename =“temp”+ cnt +“.jpg”;
cnt ++;

所以我的混淆是“是否有加载图像视图从字符串(imagename变量)像temp1.jpg等”

回答

3

我下面的解决方案来实现,它的工作对我来说:

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

我不知道这是否是最佳解决方案,但是您可以制作将映像名称映射到资源的散列表。

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

然后你可以从drawable加载ImageView。

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

这意味着你需要在每次添加新的图像时编辑哈希表... – xil3 2010-07-07 09:23:16

+0

这就是为什么我写它不是最好的解决方案。虽然,您可以使用Reflections读取公共字段并自动加载hashmap。想想看,你可以使用Reflections来获取id并忘记地图。 – Itsik 2010-07-07 09:41:28

4

你可以试试这个:

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

希望这就是你要找的人。

+0

对不起...它不工作... imageview仅与空白屏幕displyed ..不与图像 – 2010-07-07 09:48:46

+0

你确定你提供正确的路径图像?在SD卡上的图像? – xil3 2010-07-07 09:54:47

+0

你也可以发布你的ImageView(XML或如果你以编程方式声明)? – xil3 2010-07-07 09:56:06

2

为何不像

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

thanx为答案.... – 2010-07-07 10:18:37