您的位置:首页 > 编程语言 > Java开发

android imageView 获取指定名字的图片、从MyEclipse服务器下载图片

2016-02-28 01:30 691 查看
设置指定名字的图片;

int imgId = getResources().getIdentifier(imgName, "drawable", getPackageName());   //imgName为图片名称;
imageView.setImageResource(imgId);


所以当有一组图片要显示时(图片以a1、a2、a3……命名)可以用for循环:

int[] imgs = new int[7];
for(int i=0; i<imgs.size(); i++)
{
imgs[i] = getResources().getIdentifier("a"+i, "drawable", getPackageName());
}


从MyEclipse服务器上下载图片;

private Bitmap getImage(String imgPath)
{
URL url = null;
Bitmap bitmap = null;
InputStream input = null;
HttpURLConnection httpCon = null;
//服务器地址+项目名+图片的路径;http://192.168.18.145:8080/Order/images/a.png (images/a.png是在服务器的WebRoot文件夹下);
url = new URL(Content.path + imgPath);
httpCon = (HttpURLConnection) url.openConnection();
httpCon.setConnectTimeout(5000);  //设置超时时间;
System.out.println("=============");
if(httpCon.getResponseCode() == 200)
{
System.out.println("----------=");
input = httpCon.getInputStream();
bitmap = BitmapFactory.decodeStream(input);
System.out.println("=====" + bitmap);
}
//获取失败的话,就显示本地的任意一张图片;
else
{
bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.dish_no_image);
}

if(input != null)
{
input.close();
}
if(httpCon != null)
{
httpCon.disconnect();
}
return bitmap;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: