在Facebook上发布照片

时间:2013-06-21 10:05:58

标签: android facebook

我正在尝试使用Andoid在我的FaceBook SDK应用程序中分享照片。我做完了

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 个答案:

没有答案