Android 如何根据网络地址获取网络图片方法

<h2><pre name="code" class="html" style="font-weight: bold; font-size: 24px;">

 
 

一、注意点:连接对象获取,请求方法“GET”,资源获取超时设置,建立连接,通过连接获取输入流,采用谷歌API:BitmapFactory得到图片对象Bitmap。

public Bitmap getInternetPicture(String UrlPath) {
		Bitmap bm = null;
		// 1、确定网址
		// http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg
		String urlpath = UrlPath;
		// 2、获取Uri
		try {
			URL uri = new URL(urlpath);

			// 3、获取连接对象、此时还没有建立连接
			HttpURLConnection connection = (HttpURLConnection) uri.openConnection();
			// 4、初始化连接对象
			// 设置请求的方法,注意大写
			connection.setRequestMethod("GET");
			// 读取超时
			connection.setReadTimeout(5000);
			// 设置连接超时
			connection.setConnectTimeout(5000);
			// 5、建立连接
			connection.connect();

			// 6、获取成功判断,获取响应码
			if (connection.getResponseCode() == 200) {
				// 7、拿到服务器返回的流,客户端请求的数据,就保存在流当中
				InputStream is = connection.getInputStream();
				// 8、从流中读取数据,构造一个图片对象GoogleAPI
				bm = BitmapFactory.decodeStream(is);
				// 9、把图片设置到UI主线程
				// ImageView中,获取网络资源是耗时操作需放在子线程中进行,通过创建消息发送消息给主线程刷新控件;

				Log.i("", "网络请求成功");

			} else {
				Log.v("tag", "网络请求失败");
				bm = null;
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bm;

	}


二、 同时要注意网络操作需在子线程操作,以免引起主线程阻塞,影响用途体验,同时采用handler消息机制进行参数处理,刷新UI控件。

public void onClick(View v){
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				String urlpath = "http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg";
				Bitmap bm = getInternetPicture(urlpath);
				Message msg = new Message();
				// 把bm存入消息中,发送到主线程
				msg.obj = bm;
				handler.sendMessage(msg);
			}
		}).start();
	}</span>

三、 主线程处理消息队列中的消息,并刷新相应UI控件


Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			ImageView imgView = (ImageView) findViewById(R.id.internet_imageview);
			imgView.setImageBitmap((Bitmap) msg.obj);
		};
	};</span>

四、获取网络图片,采用缓存保存文件

<span style="font-size:18px;">public Bitmap getInternetPicture(String UrlPath) {
		Bitmap bm = null;
		// 1、确定网址
		// http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg
		String urlpath = UrlPath;
		// 2、获取Uri
		try {
			URL uri = new URL(urlpath);

			// 3、获取连接对象、此时还没有建立连接
			HttpURLConnection connection = (HttpURLConnection) uri.openConnection();
			// 4、初始化连接对象
			// 设置请求的方法,注意大写
			connection.setRequestMethod("GET");
			// 读取超时
			connection.setReadTimeout(5000);
			// 设置连接超时
			connection.setConnectTimeout(5000);
			// 5、建立连接
			connection.connect();

			// 6、获取成功判断,获取响应码
			if (connection.getResponseCode() == 200) {
				// 7、拿到服务器返回的流,客户端请求的数据,就保存在流当中
				InputStream is = connection.getInputStream();
				// 8、开启文件输出流,把读取到的字节写到本地缓存文件
				File file = new File(getCacheDir(), getFileName(urlpath));
				FileOutputStream fos = new FileOutputStream(file);
				int len = 0;
				byte[] b = new byte[1024];
				while ((len = is.read(b)) != -1) {
					fos.write(b, 0, len);
				}
				fos.close();
				is.close();
				//9、 通过图片绝对路径,创建Bitmap对象

				bm = BitmapFactory.decodeFile(file.getAbsolutePath());

				Log.i("", "网络请求成功");

			} else {
				Log.v("tag", "网络请求失败");
				bm = null;
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bm;

	}

	public String getFileName(String path) {
		int index = path.lastIndexOf("/");
		return path.substring(index + 1);
	}
}</span>





  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值