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); 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *