使用适用于Android的Facebook SDK在Facebook时间轴上发布图片

时间:2016-05-30 13:26:56

标签: android facebook sdk

我有一个Android应用程序将用户的状态消息发布到她/他在Facebook上的时间线(请参阅下面发布消息的代码片段),问题是我无法发布任何状态图像,如表情符号。我尝试将图像作为字节数组发送(有人建议在另一篇文章中)无效,图片被拒绝,如果我发布图像的URL,Facebook会插入URL而不是实际图像。有没有办法将图像张贴为真实图片?我使用Facebook SDK 4.11 for Android和Android Studio 2.1。任何提示都将受到欢迎。谢谢

这是代码:

postParams.putString("caption", getResources().getString(R.string.app_name)); // text to post
postParams.putString("message", msg);
postParams.putString("link", "https://www.terra7.net");

if (imgUrl != null) {

    //Bitmap bi = BitmapFactory.decodeResource(getResources(), imgId);
    //ByteArrayOutputStream baos = new ByteArrayOutputStream();
    //bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
    //byte[] data = baos.toByteArray();

    //postParams.putByteArray("picture", data); // image to post
    postParams.putString("picture", imgUrl); // image to post
}

GraphRequest request = new GraphRequest(accessToken, "me/feed", postParams, HttpMethod.POST, new GraphRequest.Callback() {
    @Override
    public void onCompleted(GraphResponse response) {
        if (response.getError() != null) {
            Toast.makeText(MoodFragment.this.getContext(), response.getError().getErrorMessage(), Toast.LENGTH_LONG).show();
        }
    }
});
request.executeAsync();

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

public void sharePhotoToFacebook() {

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
        SharePhoto photo = new SharePhoto.Builder()
                .setBitmap(bitmap)
                .build();
        SharePhotoContent content = new SharePhotoContent.Builder()
                .addPhoto(photo)
                .build();
        ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
            @Override
            public void onSuccess(Sharer.Result result) {
                Log.e("onSuccess ", "result " + result);
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {
                Log.e("onError ", "error " + error);
            }
        });
    }

要使用上述代码,您需要使用publish_actions权限,您可以使用以下代码获取权限:

public void getPermission() {
        List<String> permissionNeeds = Arrays.asList("publish_actions");// - See more at: http://simpledeveloper.com/how-to-share-an-image-on-facebook-in-android/#sthash.v7b5g7GA.dpuf
        loginManager = LoginManager.getInstance();
        loginManager.logInWithPublishPermissions(this, permissionNeeds);
        loginManager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                sharePhotoToFacebook();
            }

            @Override
            public void onCancel() {
                System.out.println("onCancel");
            }

            @Override
            public void onError(FacebookException exception) {
                System.out.println("onError");
            }
        });
        // - See more at: http://simpledeveloper.com/how-to-share-an-image-on-facebook-in-android/#sthash.v7b5g7GA.dpuf


    }