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

[亡魂溺海] 提交于 2019-12-12 09:58:14

问题


If I use an Intent with ACTION_SEND and type "text/plain" and a EXTRA_TEXT Facebook doesn't prefill anything. That's something I've already seen. Every one says, use the Facebook SDK but I don't want my app to post anything automatically nor handle login tokens from my app. I just want the Write Post Facebook screen to be opened with a pre defined text, link and an Image. Just like when I share an image from the Gallery app. Is it possible?


回答1:


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();
}

}



来源:https://stackoverflow.com/questions/15409920/how-to-open-the-facebook-write-post-with-some-pre-defined-text-and-image

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