专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 图形/图像

ImageView访问服务器下的图片资源

发布时间:2011-06-27 20:28:57 文章来源:www.iduyao.cn 采编人员:星星草
ImageView访问服务器上的图片资源

试了  

image = (ImageView) findViewById(R.id.img);

image.setImageURI(Uri.parse("http://XXXX/Test/index/4.png")); 方法,不知道是什么原因,图片总是显示不出来

 

下面方法可以将服务器端的图片显示到ImageView上

 

		try {
			image.setImageBitmap(GetServerBitmap(new URL(
					"http://XXXXX/Test/index/2.jpg")));
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

 

GetServerBitmap方法

 

public static Bitmap GetServerBitmap(URL url) throws IOException {

		URLConnection conn = url.openConnection();
		try {
			conn.connect();
			InputStream is = conn.getInputStream();
			Bitmap bmp = BitmapFactory.decodeStream(is);
			is.close();
			return bmp;
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}

	}

 

 

同时记得在AndroidManifest.xml中申明访问权限,这步很重要,最初就是忘记了在里面申明访问为网络资源的权限,调试了很久

 

<uses-permission android:name="android.permission.INTERNET"/>
 
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: