Android 网络图片显示


向ImageManage中传一个图片url 

  1. import java.io.BufferedInputStream;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.net.HttpURLConnection;  
  5. import java.net.MalformedURLException;  
  6. import java.net.URL;  
  7.   
  8. import Android.graphics.Bitmap;  
  9. import android.graphics.BitmapFactory;  
  10. import android.util.Log;  
  11.   
  12. public class ImageManage  
  13. {  
  14.     public static Bitmap getImage(String url)  
  15.     {  
  16.         URL imageUrl = null;  
  17.         Bitmap bitmap = null;  
  18.           
  19.         try  
  20.         {  
  21.             imageUrl = new URL(url);  
  22.         } catch (MalformedURLException e)  
  23.         {  
  24.             Log.e("url error""url======:"+url);  
  25.         }  
  26.         try  
  27.         {  
  28.             HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();  
  29.             conn.setDoInput(true);  
  30.             conn.connect();  
  31.             InputStream is = conn.getInputStream();  
  32.             BufferedInputStream bis = new BufferedInputStream(is);  
  33.             bitmap = BitmapFactory.decodeStream(bis);  
  34.             bis.close();  
  35.             is.close();  
  36.         } catch (IOException e)  
  37.         {  
  38.             e.printStackTrace();  
  39.             Log.e("connect error""连接失败");  
  40.         }  
  41.           
  42.         return bitmap;  
  43.     }  
  44.   
  45. }  

相关内容