2

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?

Héctor Júdez Sapena
  • 5,329
  • 3
  • 23
  • 31

1 Answers1

2

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

}

Héctor Júdez Sapena
  • 5,329
  • 3
  • 23
  • 31