歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 網絡圖片顯示

Android 網絡圖片顯示

日期:2017/3/1 11:15:07   编辑:Linux編程

向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. import Android.graphics.Bitmap;
  8. import android.graphics.BitmapFactory;
  9. import android.util.Log;
  10. public class ImageManage
  11. {
  12. public static Bitmap getImage(String url)
  13. {
  14. URL imageUrl = null;
  15. Bitmap bitmap = null;
  16. try
  17. {
  18. imageUrl = new URL(url);
  19. } catch (MalformedURLException e)
  20. {
  21. Log.e("url error", "url======:"+url);
  22. }
  23. try
  24. {
  25. HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
  26. conn.setDoInput(true);
  27. conn.connect();
  28. InputStream is = conn.getInputStream();
  29. BufferedInputStream bis = new BufferedInputStream(is);
  30. bitmap = BitmapFactory.decodeStream(bis);
  31. bis.close();
  32. is.close();
  33. } catch (IOException e)
  34. {
  35. e.printStackTrace();
  36. Log.e("connect error", "連接失敗");
  37. }
  38. return bitmap;
  39. }
  40. }
Copyright © Linux教程網 All Rights Reserved