2014-10-26 149 views
2

在我的應用程序中,我讓用戶從Facebook上傳照片。所以首先我獲取所有用戶相冊:獲取Facebook相冊和封面照片

new Request(session, "/me/albums", null, HttpMethod.GET, new Request.Callback() { 
      public void onCompleted(Response response) { 
} 
}; 

,然後我用一個適配器來顯示GridView控件中的每一張專輯,所以getView方法我稱之爲/{album-id}/picture裏面,但我不知道如何解析響應。我與Response對象有什麼關係來在ImageView中顯示圖像?有沒有更好的方法來做到這一點?

這是我提出的要求

   Bundle params = new Bundle(); 
       params.putString("type", "thumbnail"); 
       String albumid = albums.get(position); 
       new Request(session, "/"+ albumid +"/picture", 
         params, HttpMethod.GET, 
         new Request.Callback() { 
        public void onCompleted(Response response) { 

         ViewHolder holder = new ViewHolder(); 
        } 
       } 
         ).executeAsync(); 

而這就是我得到:

{Response: responseCode: 200, graphObject: GraphObject{graphObjectClass=GraphObject, state={"FACEBOOK_NON_JSON_RESULT":"����\u0000\u0010JFIF\u0000\u0001\u0002\u0000\u0000\u0001\u0000\u0001\u0000\u0000��\u0000\u0004*\u0000��\u0002\u001cICC_PROFILE\u0000\u0001\u0001\u0000\u0000\u0002"}}, error: null, isFromCache:false}

回答

0

你最有可能取回一個HTTP重定向到圖片的網址(這將是例如可用於網站上的<img>標籤)

這記錄在Album documentation中:

Fields No fields are returned, instead you will receive the image file itself.

什麼你可能需要做的是特別要求的封面照片,與像一個電話:

/<version>/<album id>?fields=cover_photo - 一旦你的ID,你可以請求Photo object的詳細信息,並採取最合適的尺寸從所述響應

+0

但是,如果我想顯示所有專輯名稱和封面的網格,我將不得不提出每個專輯的請求,以便從照片對象獲取網址,然後下載照片? – 2014-10-29 07:37:50

+0

您可以使用/圖片獲取圖片的字節(就像您現在所做的那樣,但您的代碼需要一個字符串)或者批量獲取圖片網址,並以與您相同的方式將其加載到視圖中試圖在你的示例代碼 – Igy 2014-10-29 16:33:14

1

用於取入的Facebook相冊源圖像使用此

Bundle params = new Bundle(); 
       //params.putString("fields", "photos.limit(10),name,count"); 
       params.putString("access_token", facebook.getAccessToken()); 
       params.putString("limit", "1000"); 
       //me = new JSONObject(facebook.request("me/albums",params ,"GET")); 
       System.out.println("params are: "+params); 

       if(facebook.getAccessToken()!= null) 
       { 
        me = new JSONObject(facebook.request("me/albums",params ,"GET")); 
       } 

       fbAlbumList.clear(); 
       try { 
        //JSONObject jsonObject = new JSONObject(respons); 
        JSONArray jsonArray = me.getJSONArray("data"); 
        for (int i = 0; i < jsonArray.length(); i++) { 
         FbAlbumBean bean = new FbAlbumBean(); 
         bean.albumId = jsonArray.getJSONObject(i).getString("id"); 
         bean.albumName = jsonArray.getJSONObject(i).getString("name"); 
         if(i!=0) 
         { 
         bean.coverId = jsonArray.getJSONObject(i).getString("cover_photo"); 
         bean.albumCount = jsonArray.getJSONObject(i).getInt("count")+""; 
         } 
         fbAlbumList.add(bean); 
        } 

用以取入照片b y專輯Id

public void fetchPhotosFromAlbumId(final String album_id, Activity activity){ 

    //new FetchAsyncTask().execute(album_id); 

    progressDialog1 = ProgressDialog.show(activity,"", "Please wait..."); 
    System.out.println("progress dialog: "+progressDialog1); 

    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      Looper.prepare(); 

      JSONObject me; 
      try { 
       fbPhotosList.clear(); 
       String offset1 = String.valueOf(offset+100); 
       Bundle params = new Bundle(); 
       params.putString("limit", "1000"); 
       params.putString("access_token", facebook.getAccessToken()); 

       //me = new JSONObject(facebook.request("me/albums",params ,"GET")); 
       me = new JSONObject(facebook.request(album_id+"/photos",params ,"GET")); 
       System.out.println(me); 

       JSONArray jsonArray = me.getJSONArray("data"); 
       for (int i = 0; i < jsonArray.length(); i++) { 
        CameraRollBean bean = new CameraRollBean(); 
        bean.filePath = jsonArray.getJSONObject(i).getString("source"); 
        bean.showRight_IB = false; 
        fbPhotosList.add(bean); 
       } 
       System.out.println("size of fb album: "+fbAlbumList.size()); 
       FbAlbumsScreen.fromFbScreen = true; 
       Intent intent = new Intent(_activity, CameraRollScreen.class); 
       intent.putExtra("album_id", album_id); 
       _activity.startActivity(intent); 


       if(progressDialog1!=null) 
        progressDialog1.cancel(); 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      }catch (Exception e){ 
       e.printStackTrace(); 
      } 
     } 
    }).start(); 

    progressDialog1.show(); 
} 
+0

偉大,但你如何獲取位於bean.coverId的圖像?我需要用他的封面來展示每一張專輯。 – 2014-11-03 11:56:00