2013-01-24 79 views
0

我正在使用Facebook API做一個Android應用程序。它要求用戶選擇朋友以查看標記的照片。 (如果用戶選擇了兩個名字A和B - >他應該能夠在一張照片中看到A和B)。爲了實現這一目標,我使用圖形api資源管理器,但發現很難提出請求調用。請參考圖片:Facebook標籤在Android的照片 - > Facebook的SDK

facebook api explorer

對於我的查詢,上面應該使用的信息。也就是說,當我提供「id(userID)」時,我需要得到「圖片」。

請告訴我這是怎麼做的!

+0

你的問題不清楚給我。你是否真的想用標記的朋友張貼圖片,或者你只是想在圖片中添加好友? – Jan

回答

0

有涉及這個五個步驟:

步驟1:

登錄至Facebook:

private void loginToFacebook() 
     { 
      mPrefs = activity.getPreferences(Context.MODE_PRIVATE); 
      facebook = new Facebook(APP_ID); 
      accessToken = mPrefs.getString("access_token", null); 
      long expires = mPrefs.getLong("access_expires", 0); 

      if (accessToken != null) 
      { 
       facebook.setAccessToken(accessToken); 
       uploadPhoto(facebook, imageBitmap, null); 
      } 

      if (expires != 0) 
      { 
       facebook.setAccessExpires(expires); 
      } 

      if (!facebook.isSessionValid()) 
      { 
      facebook.authorize(activity, new String[] {"publish_stream"}, Facebook.FORCE_DIALOG_AUTH, new DialogListener() { 

        public void onFacebookError(FacebookError arg0) { 
         // TODO Auto-generated method stub 

        } 

        public void onError(DialogError arg0) { 
         // TODO Auto-generated method stub 

        } 

        public void onComplete(Bundle arg0) { 
          SharedPreferences.Editor editor = mPrefs.edit(); 
          editor.putString("access_token", facebook.getAccessToken()); 
          editor.putLong("access_expires", facebook.getAccessExpires()); 
          editor.commit();       
          uploadPhoto(facebook, imageBitmap, null);     
        } 

        public void onCancel() { 
         // TODO Auto-generated method stub 

        } 
       }); 

      } 
     } 

步驟2:

將圖像上傳至Facebook(位圖被上傳),請注意,此方法在登錄方法中調用。下面的方法返回上傳的圖片的ID,所以這是需要標記的朋友時:

public String uploadPhoto(Facebook facebook, Bitmap image, String albumId) 
     { 
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 

      if (albumId == null) { 
       albumId = "me"; 
      } 

      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      image.compress(CompressFormat.JPEG, 75, bos); 
      byte[] photoBytes = bos.toByteArray(); 

      Bundle params = new Bundle(); 
      params.putString(facebook.TOKEN, accessToken); 
      params.putByteArray("picture", photoBytes); 

      try { 
       @SuppressWarnings("deprecation") 
       String resp = facebook.request(albumId + "/photos", params, "POST"); 
       JSONObject json = Util.parseJson(resp); 
       if(json.getString("id") != null){ 
        Toast.makeText(activity, "Image Successfully uploaded to facebook!", Toast.LENGTH_SHORT).show(); 

        tagFriends(); 

       }else{ 
        Toast.makeText(activity, "Uploading failed, please try again!", Toast.LENGTH_SHORT).show(); 

       } 
       uploadedImageID = json.getString("id"); 
       return json.getString("id"); 

      } catch (IOException e) { 
       Toast.makeText(activity, "Uploading failed, please try again!", Toast.LENGTH_SHORT).show(); 

      } catch (FacebookError e) { 
       Toast.makeText(activity, "Uploading failed, please try again!", Toast.LENGTH_SHORT).show(); 

      } catch (JSONException e) { 
       Toast.makeText(activity, "Uploading failed, please try again!", Toast.LENGTH_SHORT).show(); 

      } 

      return null; 
     } 

第3步:

獲取您的Facebook帳戶和存儲的所有朋友在哈希地圖:

Bundle params = new Bundle(); 
       params.putString(Facebook.TOKEN, accessToken); 
       mAsyncRunner.request("me/friends" , params , "GET", new RequestListener() 
       { 

        @Override 
        public void onMalformedURLException(MalformedURLException e, Object state) 
        { 

        } 

        @Override 
        public void onIOException(IOException e, Object state) 
        { 

        } 

        @Override 
        public void onFileNotFoundException(FileNotFoundException e, Object state) 
        { 

        } 

        @Override 
        public void onFacebookError(FacebookError e, Object state) 
        { 

        } 

        @Override 
        public void onComplete(String response, Object state) 
        { 
         try 
         { 
          JSONObject responseJsonObject = new JSONObject(response); 
          Log.d("InviteFriends.inviteFriends().new RequestListener() {...}:onComplete", "FB Response =>"+ responseJsonObject); 
          //event_id = event.getString("id"); 


          //JSONObject responseJsonObject = new JSONObject(eventResponse); 
          JSONArray jsonArray = responseJsonObject.getJSONArray("data"); 


          for (int i = 0; i < jsonArray.length(); i++) 
          { 
           HashMap<String, String> map = new HashMap<String, String>(); 
           JSONObject e = jsonArray.getJSONObject(i); 

           map.put("id", e.getString("id")); 
           map.put("name", e.getString("name")); 
           mylist.add(map); 

           userIds = e.getString("id"); 
           userName = e.getString("name"); 
           Log.d("MainActivity:getAllEvents", "Friend ID, Name:" + userIds + "," + userName); 

          } 
         } 
         catch (Exception e) 
         { 
          Log.e("log_tag", "Error parsing data "+e.toString()); 
         } 

步驟4:

顯示名稱列表中列表視圖,並捕獲該用戶從列表中觸摸的用戶名的用戶ID,並將它們存儲在一個陣列:

final ListView friends = (ListView) findViewById(R.id.lst_frnds); 
ListAdapter adapter = new SimpleAdapter(getApplicationContext(), mylist , R.layout.friends_main, new String[] {"name" }, new int[] {R.id.item_title}); 
           Log.d("size","Friends->" + mylist.size()); 
friends.setAdapter(adapter); 
friends.setOnItemClickListener(new OnItemClickListener() { 
            public void onItemClick(final AdapterView<?> parent, View view, int position, long id) { 
             @SuppressWarnings("unchecked") 
             HashMap<String, String> o = (HashMap<String, String>) friends.getItemAtPosition(position); 
             String selectedName = o.get("name"); 
             String selectedId = o.get("id"); 
             Toast.makeText(MemeList.this, "Your friend " + selectedName + "' was selected.", Toast.LENGTH_SHORT).show(); 

             selectedFriendsIDs.add(selectedId); 
             selectedFriendsNames.add(selectedName); 
} 

第5步:

標籤選擇朋友列表:

btnSendInvites.setText(selectedFriendsIDs.size() + " selected, tag them now?"); 



             btnSendInvites.setOnClickListener(new OnClickListener() { 

              @Override 
              public void onClick(View v) { 

               //Tagging friends 
               for(int i = 0; i < selectedFriendsIDs.size(); i++){ 

                String inputParm = uploadedImageID + "/tags?to=" + selectedFriendsIDs.get(i); 

                Bundle params = new Bundle(); 
                params.putString(Facebook.TOKEN, accessToken); 
                mAsyncRunner.request(inputParm, params, "POST", new RequestListener() 
                { 

                 @Override 
                 public void onMalformedURLException(MalformedURLException e, Object state) 
                 { 

                 } 

                 public void onIOException1(IOException e, Object state) 
                 { 

                 } 

                 @Override 
                 public void onFileNotFoundException(FileNotFoundException e, Object state) 
                 { 

                 } 

                 @Override 
                 public void onFacebookError(FacebookError e, Object state) 
                 { 

                 } 

                 @Override 
                 public void onComplete(String response, Object state) 
                 { 
                  Log.d("Selected IDs ", "" + selectedFriendsIDs); 
                 } 

                 @Override 
                 public void onIOException(IOException e, Object state) 
                 { 
                  // TODO Auto-generated method stub 

                 } 
                }, null); 
               } 


              } 
             }); 

如果成功標記,將返回true。

希望這可以幫助別人!