Android手机开发:从网络上读取图片


程序:

  1.         /** 
  2.  * 根据图片的网络地址url,获取图片 
  3.  * 添加权限 
  4.  * <uses-permission Android:name="android.permission.INTERNET" /> 
  5.  */  
  6. public static Bitmap GetNetBitmap(String url) {  
  7.     URL imageUrl = null;  
  8.     Bitmap bitmap = null;  
  9.       
  10.     try {  
  11.         imageUrl = new URL(url);  
  12.     }catch(MalformedURLException e) {  
  13.         Log.e("MalformedURLException", e.getMessage());  
  14.     }  
  15.       
  16.     try {  
  17.         HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();  
  18.         conn.setDoInput(true);  
  19.         //将得到的数据转换成InputStream   
  20.         InputStream is = conn.getInputStream();  
  21.         //将InputStream转换成Bitmap   
  22.         bitmap = BitmapFactory.decodeStream(is);  
  23.         is.close();  
  24.     }catch(IOException e) {  
  25.         e.printStackTrace();  
  26.         Log.e("IOException", e.getMessage()+"\n");  
  27.     }  
  28.     return bitmap;  
  29. }  

相关内容