2013-06-21 151 views
1

我正在嘗試分享照片從我的Andoid應用程序在Facebook上使用FaceBook SDK的。我已經完成在Facebook上發佈圖片

Facebook fb; 
String APP_ID="xxxxx";//xxxxx: is my app id 
private static final String[] PERMISSIONS = new String[] { "publish_stream" }; 
private static final String TOKEN = "access_token"; 
private static final String EXPIRES = "expires_in"; 
private static final String KEY = "facebook-credentials"; 
mShareButton= (Button) findViewById(R.id.share_button); 
mShareButton.setOnClickListener(new View.OnClickListener() { 
public void onClick(View v) { 

      restoreCredentials(fb); 
      messageToPost = "Hello Everyone."; 
      if (!fb.isSessionValid()) { 
       loginAndPostToWall(); 


      } 
public boolean restoreCredentials(Facebook facebook) { 
    SharedPreferences sharedPreferences = getApplicationContext() 
      .getSharedPreferences(KEY, Context.MODE_PRIVATE); 
    facebook.setAccessToken(sharedPreferences.getString(TOKEN, null)); 
    facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0)); 
    return facebook.isSessionValid(); 
} 

public void loginAndPostToWall() { 
    fb.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, 
      new LoginDialogListener()); 
} 

public void postPhotoToWall() { 
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.melody); 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
    final byte[] data = stream.toByteArray(); 

    Bundle parameters = new Bundle(); 
    parameters.putString("message", "Message"); 
    parameters.putByteArray("picture", data); 
    parameters.putString("caption", "test"); 

    try { 
     Log.i("Tests", "got response: "); 
     fb.request("me/feed"); 
     Log.i("Tests", "got response: "); 

     String response = fb.request("me/feed", parameters, "POST"); 
     Log.d("Tests", "got response: " + response); 
     if (response == null || response.equals("") || response.equals("false")) { 
      showToast("Blank response."); 
     } else { 
      showToast("Photo posted to your facebook wall!"); 
     } 
    } catch (Exception e) { 
     showToast("Failed to post photo to your facebook wall!"); 
     e.printStackTrace(); 
    } 
} 

當我點擊分享按鈕時,我可以連接到我的帳戶,我從中獲得應用程序ID。此外,我無法將照片發佈到牆上:我剛剛獲得「無法將照片發佈到您的Facebook牆上!」。我找不出這個問題,請幫忙。

回答

0

仔細查閱這個環節,我已經在這裏提供的示例代碼:http://sdrv.ms/1a6SrOG

+0

我有一個代碼提供身份證。我須張貼U中的代碼 – KOTIOS

+0

參照上述 – KOTIOS

+0

我更新的代碼我有新的WallPostListener錯誤()。 –