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

 

[html] view plain copy
 
  1. <h2><pre name="code" class="html" style="font-weight: bold; font-size: 24px;">  


 
 

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

 
[java] view plain copy
 
  1. public Bitmap getInternetPicture(String UrlPath) {  
  2.         Bitmap bm = null;  
  3.         // 1、确定网址  
  4.         // http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg  
  5.         String urlpath = UrlPath;  
  6.         // 2、获取Uri  
  7.         try {  
  8.             URL uri = new URL(urlpath);  
  9.   
  10.             // 3、获取连接对象、此时还没有建立连接  
  11.             HttpURLConnection connection = (HttpURLConnection) uri.openConnection();  
  12.             // 4、初始化连接对象  
  13.             // 设置请求的方法,注意大写  
  14.             connection.setRequestMethod("GET");  
  15.             // 读取超时  
  16.             connection.setReadTimeout(5000);  
  17.             // 设置连接超时  
  18.             connection.setConnectTimeout(5000);  
  19.             // 5、建立连接  
  20.             connection.connect();  
  21.   
  22.             // 6、获取成功判断,获取响应码  
  23.             if (connection.getResponseCode() == 200) {  
  24.                 // 7、拿到服务器返回的流,客户端请求的数据,就保存在流当中  
  25.                 InputStream is = connection.getInputStream();  
  26.                 // 8、从流中读取数据,构造一个图片对象GoogleAPI  
  27.                 bm = BitmapFactory.decodeStream(is);  
  28.                 // 9、把图片设置到UI主线程  
  29.                 // ImageView中,获取网络资源是耗时操作需放在子线程中进行,通过创建消息发送消息给主线程刷新控件;  
  30.   
  31.                 Log.i("", "网络请求成功");  
  32.   
  33.             } else {  
  34.                 Log.v("tag", "网络请求失败");  
  35.                 bm = null;  
  36.             }  
  37.         } catch (MalformedURLException e) {  
  38.             e.printStackTrace();  
  39.         } catch (IOException e) {  
  40.             e.printStackTrace();  
  41.         }  
  42.         return bm;  
  43.   
  44.     }  


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

[html] view plain copy
 
  1. public void onClick(View v){  
  2.         new Thread(new Runnable() {  
  3.               
  4.             @Override  
  5.             public void run() {  
  6.                 // TODO Auto-generated method stub  
  7.                 String urlpath = "http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg";  
  8.                 Bitmap bm = getInternetPicture(urlpath);  
  9.                 Message msg = new Message();  
  10.                 // 把bm存入消息中,发送到主线程  
  11.                 msg.obj = bm;  
  12.                 handler.sendMessage(msg);  
  13.             }  
  14.         }).start();  
  15.     }</span>  

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

 
[html] view plain copy
 
  1. Handler handler = new Handler() {  
  2.         public void handleMessage(android.os.Message msg) {  
  3.   
  4.             ImageView imgView = (ImageView) findViewById(R.id.internet_imageview);  
  5.             imgView.setImageBitmap((Bitmap) msg.obj);  
  6.         };  
  7.     };</span>  

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

[java] view plain copy
 
  1. <span style="font-size:18px;">public Bitmap getInternetPicture(String UrlPath) {  
  2.         Bitmap bm = null;  
  3.         // 1、确定网址  
  4.         // http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg  
  5.         String urlpath = UrlPath;  
  6.         // 2、获取Uri  
  7.         try {  
  8.             URL uri = new URL(urlpath);  
  9.   
  10.             // 3、获取连接对象、此时还没有建立连接  
  11.             HttpURLConnection connection = (HttpURLConnection) uri.openConnection();  
  12.             // 4、初始化连接对象  
  13.             // 设置请求的方法,注意大写  
  14.             connection.setRequestMethod("GET");  
  15.             // 读取超时  
  16.             connection.setReadTimeout(5000);  
  17.             // 设置连接超时  
  18.             connection.setConnectTimeout(5000);  
  19.             // 5、建立连接  
  20.             connection.connect();  
  21.   
  22.             // 6、获取成功判断,获取响应码  
  23.             if (connection.getResponseCode() == 200) {  
  24.                 // 7、拿到服务器返回的流,客户端请求的数据,就保存在流当中  
  25.                 InputStream is = connection.getInputStream();  
  26.                 // 8、开启文件输出流,把读取到的字节写到本地缓存文件  
  27.                 File file = new File(getCacheDir(), getFileName(urlpath));  
  28.                 FileOutputStream fos = new FileOutputStream(file);  
  29.                 int len = 0;  
  30.                 byte[] b = new byte[1024];  
  31.                 while ((len = is.read(b)) != -1) {  
  32.                     fos.write(b, 0, len);  
  33.                 }  
  34.                 fos.close();  
  35.                 is.close();  
  36.                 //9、 通过图片绝对路径,创建Bitmap对象  
  37.   
  38.                 bm = BitmapFactory.decodeFile(file.getAbsolutePath());  
  39.   
  40.                 Log.i("", "网络请求成功");  
  41.   
  42.             } else {  
  43.                 Log.v("tag", "网络请求失败");  
  44.                 bm = null;  
  45.             }  
  46.         } catch (MalformedURLException e) {  
  47.             e.printStackTrace();  
  48.         } catch (IOException e) {  
  49.             e.printStackTrace();  
  50.         }  
  51.         return bm;  
  52.   
  53.     }  
  54.   
  55.     public String getFileName(String path) {  
  56.         int index = path.lastIndexOf("/");  
  57.         return path.substring(index + 1);  
  58.     }  
  59. }</span>  


 
 
原文地址:https://www.cnblogs.com/xunbu7/p/6501476.html