我正在通过Android中的共享对话框在FB上共享视频。共享工作正常。但是,FB帖子ID返回null。即使在上传视频之前,回调也会返回。如果我错过了什么,请告诉我。下面是我的代码。

public class TestFragment extends Fragment {

    private CallbackManager callbackManager;
    private ShareDialog shareDialog;

    public TestFragment() {
        // Required empty public constructor
    }

    public static TestFragment newInstance(String path, String json) {
        TestFragment fragment = new TestFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FacebookSdk.sdkInitialize(getActivity());
        callbackManager = CallbackManager.Factory.create();
        shareDialog = new ShareDialog(this);
        // this part is optional
        shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
            @Override
            public void onSuccess(Sharer.Result result) {
                Timber.d("result.getPostId() :: " + result.getPostId());
            }

            @Override
            public void onCancel() {
                Timber.d("Facebook : Cancelled");
            }

            @Override
            public void onError(FacebookException e) {
                Timber.d(e.getMessage());
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_test, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @OnClick(R.id.facebookShare)
    public void share() {
        Timber.d("share button pressed");
        if (ShareDialog.canShow(ShareVideoContent.class)) {
            Timber.d("showing share dialog");
            shareDialog.show(getVideoContent());
        } else {
            Timber.d("unable to show the share dialog");
        }
    }

    private ShareVideoContent getVideoContent() {
        Timber.d(mVideoMetadata.getVideoId());

        ShareVideo shareVideo = new ShareVideo.Builder()
                .setLocalUrl(Uri.parse("... file ..."))
                .build();
        ShareVideoContent content = new ShareVideoContent.Builder()
                .setVideo(shareVideo)
                .build();

        return content;
    }

    @Override
    public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
}

最佳答案

shareDialog.show(shareContent,ShareDialog.Mode.FEED);
将模式设置为ShareDialog.Mode.FEED。
它为我工作。

Here's example

10-06 01:48