2013-10-22 77 views
0

我想使用Graph API在Facebook上發佈圖像。我已經將圖像轉換爲字節數組並嘗試發佈它,但是我失敗了。它顯示貼在牆上的文字,但圖像不張貼。我不知道什麼是錯的。使用GraphApi在Facebook上發佈圖像不起作用

我已經檢查了以下可用的解決方案,但他們都沒有爲我工作。

1)How to post image from drawable folder to facebook with graph api?

2)Unable to post an image from drawable to facebook

3)Post image with text on facebook from android

4)Android photo upload to facebook using graph api?

這裏是我試圖的代碼。

private void publishFeedDialog() { 
     Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), 
       R.drawable.ic_launcher); 

     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
     byte[] bitMapData = stream.toByteArray(); 

     final Bundle params = new Bundle(); 
     params.putString("name", "Test FB Post."); 
     params.putString("method", "photos.upload"); 
     params.putByteArray("picture", bitMapData); 

     //Tried below code but not working.   
     /*try { 
      String response = mFacebook.request("me/photos", params, "POST"); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }*/ 
     WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(m_context, 
       Session.getActiveSession(), params)).setOnCompleteListener(
       new OnCompleteListener() { 

        @Override 
        public void onComplete(Bundle values, 
          FacebookException error) { 
         if (error == null) { 
          // When the story is posted, echo the success 
          // and the post Id. 
          final String postId = values.getString("post_id"); 
          if (postId != null) { 
           Toast.makeText(m_context, 
             "Posted story, id: " + postId, 
             Toast.LENGTH_SHORT).show(); 

           finish(); 
          } else { 
           // User clicked the Cancel button 
           Toast.makeText(m_context, "Publish cancelled", 
             Toast.LENGTH_SHORT).show(); 
          } 
         } else if (error instanceof FacebookOperationCanceledException) { 
          // User clicked the "x" button 
          Toast.makeText(m_context, "Publish cancelled", 
            Toast.LENGTH_SHORT).show(); 
         } else { 
          // Generic, ex: network error 
          Toast.makeText(getApplicationContext(), 
            "Error posting story", Toast.LENGTH_SHORT) 
            .show(); 
         } 
        } 

       }).build(); 
     feedDialog.show(); 
    } 

請指導我。任何幫助將不勝感激。

感謝

+0

我已經解決了我的問題: 照片可以被創建。請檢查我的答案。 – GrIsHu

回答

0

最後我解決我的問題,使用Simple Facebook SDK for Android which wraps original Facebook SDK 3.5

該SDK提供簡單的方式來分享在Facebook上牆圖像使用其中提到的SimpleFacebook類的資源在Facebook上共享圖像。

您可以將照片發佈(上傳)到默認相冊或任何其他相冊中。

  • 位圖
  • 文件
  • 的byte []
private SimpleFacebook mSimpleFacebook; 
mSimpleFacebook = SimpleFacebook.getInstance(this); 
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
        R.drawable.ic_launcher); 
    // create Photo instace and add some properties 
Photo photo = new Photo(bitmap); 
photo.addDescription("Screenshot from sample application"); 
photo.addPlace("110619208966868"); 
// publish 
mSimpleFacebook.publish(photo, new OnPublishListener() 
{ 
    @Override 
    public void onFail(String reason) 
    { 
    mProgress.hide(); 
    // insure that you are logged in before publishing 
    Log.w(TAG, "Failed to publish"); 
    } 
    @Override 
    public void onException(Throwable throwable) 
    { 
      mProgress.hide(); 
     Log.e(TAG, "Bad thing happened", throwable); 
    } 
    @Override 
    public void onThinking() 
    { 
    // show progress bar or something to the user while publishing 
     mProgress = ProgressDialog.show(this, "Thinking", 
    "Waiting for Facebook", true); 
    } 
    @Override 
    public void onComplete(String id) 
    { 
     mProgress.hide(); 
     toast("Published successfully. The new image id = " + id); 
    } 
}); 
相關問題