How to open the Facebook Write Post with some pre defined text and image

喜欢而已 提交于 2019-12-06 11:20:39

Ok, it is impossible to do it via Intent. The only solution to show text, images + links and let the user write something before it is posted is by using the ugly Feed Dialog (or creating an custom activity with an EditText in which the user can write someting).

Here's the code that works:

public class FacebookPostActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_facebook_post);

    Session.openActiveSession(this, false, new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            if(session.isOpened()){
                publishFeedDialog();
            }
        }
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}

private void publishFeedDialog() {
    Bundle params = new Bundle();
    params.putString("name", "Facebook SDK for Android");
    params.putString("caption", "Build great social apps and get more installs.");
    params.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
    params.putString("link", "https://developers.facebook.com/android");
    params.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");

    WebDialog feedDialog = new WebDialog.FeedDialogBuilder(this, Session.getActiveSession(), params)
            .setOnCompleteListener(new OnCompleteListener() {
                @Override
                public void onComplete(Bundle values, FacebookException error) {
                    if(error == null){
                        final String postId = values.getString("post_id");
                        if(postId != null){
                            Toast.makeText(FacebookPostActivity.this, "Posted story, id: " + postId, Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(FacebookPostActivity.this, "Publish cancelled", Toast.LENGTH_SHORT).show();
                        }
                    }else if(error instanceof FacebookOperationCanceledException){
                        // User clicked the "x" button
                        Toast.makeText(FacebookPostActivity.this, "Publish cancelled", Toast.LENGTH_SHORT).show();
                    }else{
                        // Generic, ex: network error
                        Toast.makeText(FacebookPostActivity.this, "Error posting story", Toast.LENGTH_SHORT).show();
                    }
                }
            }).build();

    feedDialog.show();
}

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!