2015-02-07 50 views
2

我的問題是,我無法通過ID下載用戶頭像圖片。我可以當我使用瀏覽器來獲取用戶的個人資料圖片,這網址:無法通過Android下載Facebook用戶頭像圖片

http://graph.facebook.com/janno.hindrekson/picture?type=small

我能看到這個用戶的個人資料圖片: enter image description here

但是,當我想通過這張圖Android,我不能

// Execute the task 
    new LoadImage().execute("http://graph.facebook.com/janno.hindrekson/picture?type=small"); 


} 

private class LoadImage extends AsyncTask<String, String, Bitmap> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(TESTPROFILEPICT.this); 
     pDialog.setMessage("Loading Image ...."); 
     pDialog.show(); 
    } 

    protected Bitmap doInBackground(String... args) { 
     try { 
      bitmap = BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return bitmap; 
    } 

    protected void onPostExecute(Bitmap image) { 
     if (image != null) { 
      fbUserAvatar.setImageBitmap(image); 
      pDialog.dismiss(); 
     } else { 
      pDialog.dismiss(); 
      Toast.makeText(TESTPROFILEPICT.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

那麼有什麼想法?我應該如何通過Android下載個人資料照片?

感謝, 凱文

回答

1

我也有這個問題。嘗試用相應的facebookId替換名稱。

new LoadImage().execute("http://graph.facebook.com/100001997422983/picture?type=small"); 

編輯

我認識到了這個問題被問了幾次上計算器。

無論如何,原因是,Facebook重定向該網址。因此你必須調用重定向的url。 這是一個很好的例子:

http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/

或者你必須使用Facebook的API: https://developers.facebook.com/docs/graph-api/reference/user/picture/

我選擇了重定向消息的方法,因爲它的通用性,可以從所有獲取的圖像通過互聯網。