Facebook SDK 4.0共享回调不能调用Android

时间:2015-06-22 21:27:41

标签: android facebook-android-sdk

我有一个片段,其下一个代码是auth并将照片和视频上传到facebook:

private void postPhotoFB(int index) {
        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        if (accessToken != null) {
            if (!pathImagesFB.isEmpty() && index < pathImagesFB.size()
                    && uploadFbStatus) {
                File ff = new File(pathImagesFB.get(index));

                String format = null;
                format = URLConnection.guessContentTypeFromName(ff.getName());
                ShareApi sA;
                L("mimetype: " + format);
                if (new MimetypesFileTypeMap().getContentType(ff).toLowerCase().equals("image/png") || new MimetypesFileTypeMap().getContentType(ff).toLowerCase().equals("image/jpg")
                        || new MimetypesFileTypeMap().getContentType(ff).toLowerCase().equals("image/jpeg")) {

                    if (hasPublishPermission()) {
                        L("sending image");
                        Bitmap images = ImageUtils.getBitmap(pathImagesFB.get(index), 0);
                        SharePhoto photo = new SharePhoto.Builder().setBitmap(images).build();

                        SharePhotoContent contentPhoto = new SharePhotoContent.Builder().addPhoto(photo).build();
                        sA = new ShareApi(contentPhoto);
                        if (sA.canShare()) {
                            sA.share(shareCallback);
                        } else {
                            L("cant share photo to fb");
                        }

                    } else {
                        LoginManager.getInstance().logInWithPublishPermissions(this,
                                PUBLISH_PERMISSIONS);
                        L("share image to fb get public permission");
                    }
                } else if
                        (format.toLowerCase().equals("video/avi")) {
                    if (hasPublishPermission()) {
                        Uri videoFileUri = Uri.fromFile(ff);
                        L("share video to fb " + videoFileUri.getPath());
                        ShareVideo video = new ShareVideo.Builder()
                                .setLocalUrl(videoFileUri)
                                .build();
                        ShareVideoContent contentVideo = new ShareVideoContent.Builder()
                                .setVideo(video)
                                .build();
                        sA = new ShareApi(contentVideo);
                        if (sA.canShare()) {
                            sA.share(shareCallback);
                        } else {
                            L("cant share video to fb");
                        }

                    } else {
                        LoginManager.getInstance().logInWithPublishPermissions(this,
                                PUBLISH_PERMISSIONS);
                        L("share image to fb get public permission");
                    }
                }

            } else {
                L("postPhoto  false to facebook");
                uploadFbStatus = false;

            }
            L("facebook_uploading");
        } else {
            L("acces token is null");
        }
    }

    private ProfileTracker profileTracker;
    FacebookCallback<LoginResult> loginCallback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            L("loginCallback onSuccess");
            AccessToken a = loginResult.getAccessToken();
            L("expired " + a.getExpires());
            L("acces token login a " + a);

// ------------        **postPhotoFB(0);**                  <<<----------------HERE WORKED

        }

        @Override
        public void onCancel() {
            Log.e("NEW", "loginCallback onCancel");
        }

        @Override
        public void onError(FacebookException exception) {
            Log.e("NEW", "loginCallback onError");
            String title = "Error";
            String alertMessage = exception.getMessage();
            showResult(title, alertMessage);
        }
    };
    private FacebookCallback<Sharer.Result> shareCallback = new FacebookCallback<Sharer.Result>() {
        @SuppressLint("NewApi")
        @Override
        public void onSuccess(Sharer.Result result) {
            L("upload to Facebook Succes");
            if (result.getPostId() != null && uploadFbStatus) {

                FbImageIndex++;
                postPhotoFB(FbImageIndex);
            }
        }

        @Override
        public void onCancel() {
            Log.e("NEW", "shareCallback Canceled");
        }

        @Override
        public void onError(FacebookException error) {
            L("Facebook " + String.format("Error: %s", error.toString()));
            String title = "Error";
            String alertMessage = error.getMessage();
            showResult(title, alertMessage);
        }


    };

    private void showResult(String title, String alertMessage) {
        new AlertDialog.Builder(getActivity()).setTitle(title)
                .setMessage(alertMessage).setPositiveButton("Ok", null)
                .show();
    }

    private boolean hasPublishPermission() {
        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        return accessToken != null
                && accessToken.getPermissions().contains("publish_actions");
    }

    @SuppressLint("NewApi")
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case REQUES_FACEBOOK:
                    callbackManager.onActivityResult(requestCode, resultCode, data);
                    login_button_fb.setText(getActivity().getResources().getString(R.string.label_logout));
                    login_button_fb.setBackground(getActivity().getResources().getDrawable(R.drawable.logout));
                    break;
            }
        } else {
            L("Activitiresult NOT ok " + requestCode);
        }
    }

问题是当我从FacebookCallback()onSucces登录并调用我的post方法时(在代码中显示)它就像一个sharm,但是当我从我的代码的任何其他地方调用我的postPhotoFB方法时(比如按钮点击), shareCallback不会调用evry时间我检查我的访问令牌及其({AccessToken令牌:ACCESS_TOKEN_REMOVED权限:[public_profile,publish_actions,user_friends,email]})。

请帮助我理解我的代码中的错误,我想从我班上的任何其他地方调用我的post方法。 感谢。

1 个答案:

答案 0 :(得分:0)

而不是AccessToken accessToken = AccessToken.getCurrentAccessToken() 做 AccessToken accessToken = AccessToken.getCurrentAccessToken()。getToken()