Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android如何從服務器獲取圖片

Android如何從服務器獲取圖片

編輯:關於Android編程

[java]

public static Bitmap getBitmapFromServer(String imagePath) { 
     
    HttpGet get = new HttpGet(imagePath); 
    HttpClient client = new DefaultHttpClient(); 
    Bitmap pic = null; 
    try { 
        HttpResponse response = client.execute(get); 
        HttpEntity entity = response.getEntity(); 
        InputStream is = entity.getContent(); 
         
        pic = BitmapFactory.decodeStream(is);   // 關鍵是這句代碼  
         
    } catch (ClientProtocolException e) { 
        e.printStackTrace(); 
    } catch (IOException e) { 
        e.printStackTrace(); 
    } 
    return pic; 

 public static Bitmap getBitmapFromServer(String imagePath) {
  
  HttpGet get = new HttpGet(imagePath);
  HttpClient client = new DefaultHttpClient();
  Bitmap pic = null;
  try {
   HttpResponse response = client.execute(get);
   HttpEntity entity = response.getEntity();
   InputStream is = entity.getContent();
   
   pic = BitmapFactory.decodeStream(is);   // 關鍵是這句代碼
   
  } catch (ClientProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return pic;
 }

其中imagePath是你的圖片路徑,


最後可以將圖片顯示在手機上:


[java]
imageView.setImageBitmap(bitmap); 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved