Android-Facebook SDK:无法共享照片,对话框无法显示

前端之家收集整理的这篇文章主要介绍了Android-Facebook SDK:无法共享照片,对话框无法显示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我第一次使用 Android-Facebook SDK.我想发布一个位图,但我继续收到这条消息:
Warning: Error: com.facebook.FacebookException: Failed to copy image.

我可以发布简单的链接,但不能发布带照片的链接.

我该怎么解决这个问题?另外,我注意到的一件事是没有调用StatusCallback.call().

public abstract class FacebookReadyBaseActivity extends Activity {
    private static final String TAG = "FacebookReadyBaseActivity";

    private UiLifecycleHelper uiHelper;

    protected PendingAction pendingAction = PendingAction.NONE;
    private static final String PERMISSION = "publish_actions";
    protected boolean canPresentShareDialogWithPhotos;
    private boolean canPresentShareDialog;
    private GraphUser user;
    private GraPHPlace place;
    private List<GraphUser> tags;
    protected Bitmap pendingBitmap;

    protected enum PendingAction {
        NONE,POST_PHOTO,POST_STATUS_UPDATE
    }

    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session,SessionState state,Exception exception) {
            onSessionStateChange(session,state,exception);
        }
    };

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

        uiHelper = new UiLifecycleHelper(this,callback);
        uiHelper.onCreate(savedInstanceState);

        canPresentShareDialog = FacebookDialog.canPresentShareDialog(this,FacebookDialog.ShareDialogFeature.SHARE_DIALOG);
        canPresentShareDialogWithPhotos = FacebookDialog.canPresentShareDialog(
                this,FacebookDialog.ShareDialogFeature.PHOTOS);
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data) {
        super.onActivityResult(requestCode,resultCode,data);

        uiHelper.onActivityResult(requestCode,data,new FacebookDialog.Callback() {
                    @Override
                    public void onError(FacebookDialog.PendingCall pendingCall,Exception error,Bundle data) {
                        Log.e(TAG,String.format("Error: %s",error.toString()));
                    }

                    @Override
                    public void onComplete(
                            FacebookDialog.PendingCall pendingCall,Bundle data) {
                        Log.i(TAG,"Success!");
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        uiHelper.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }

    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }

    protected UiLifecycleHelper getFacebookUiHelper() {
        return uiHelper;
    }

    protected void performPublish(PendingAction action,boolean allowNoSession) {
        Session session = Session.getActiveSession();
        if (session != null) {
            pendingAction = action;
            if (hasPublishPermission()) {
                // We can do the action right away.
                handlePendingAction();
                return;
            } else if (session.isOpened()) {
                // We need to get new permissions,then complete the action when
                // we get called back.
                session.requestNewPublishPermissions(new Session.NewPermissionsRequest(
                        this,PERMISSION));
                return;
            }
        }

        if (allowNoSession) {
            pendingAction = action;
            handlePendingAction();
        }
    }

    private boolean hasPublishPermission() {
        Session session = Session.getActiveSession();
        return session != null
                && session.getPermissions().contains("publish_actions");
    }

    @SuppressWarnings("incomplete-switch")
    private void handlePendingAction() {
        PendingAction prevIoUslyPendingAction = pendingAction;
        // These actions may re-set pendingAction if they are still pending,but
        // we assume they
        // will succeed.
        pendingAction = PendingAction.NONE;

        switch (prevIoUslyPendingAction) {
        case POST_PHOTO:
            postPhoto();
            break;
        case POST_STATUS_UPDATE:
            postStatusUpdate();
            break;
        }
    }

    private void postPhoto() {

        if (canPresentShareDialogWithPhotos) {
            FacebookDialog shareDialog = createShareDialogBuilderForPhoto(
                    pendingBitmap).build();
            uiHelper.trackPendingDialogCall(shareDialog.present());
        } else if (hasPublishPermission()) {
            Request request = Request.newUploadPhotoRequest(
                    Session.getActiveSession(),pendingBitmap,new Request.Callback() {
                        @Override
                        public void onCompleted(Response response) {
                            InfoDialog.newInstance(
                                    FacebookReadyBaseActivity.this,"Done","Photo share finished.",null).show();
                        }
                    });
            request.executeAsync();
        } else {
            pendingAction = PendingAction.POST_PHOTO;
        }
    }

    private void postStatusUpdate() {
        if (canPresentShareDialog) {
            FacebookDialog shareDialog = createShareDialogBuilderForLink()
                    .build();
            uiHelper.trackPendingDialogCall(shareDialog.present());
        } else if (user != null && hasPublishPermission()) {
            // TODO fixme
            final String message = "Status update finished.";
            Request request = Request.newStatusUpdateRequest(
                    Session.getActiveSession(),message,place,tags,new Request.Callback() {
                        @Override
                        public void onCompleted(Response response) {

                            InfoDialog.newInstance(
                                    FacebookReadyBaseActivity.this,null).show();
                        }
                    });
            request.executeAsync();

        } else {
            pendingAction = PendingAction.POST_STATUS_UPDATE;
        }
    }

    private FacebookDialog.PhotoShareDialogBuilder createShareDialogBuilderForPhoto(
            Bitmap... photos) {
        return new FacebookDialog.PhotoShareDialogBuilder(this)
                .addPhotos(Arrays.asList(photos));
    }

    private FacebookDialog.ShareDialogBuilder createShareDialogBuilderForLink() {
        return new FacebookDialog.ShareDialogBuilder(this)
                .setName("Hello Facebook")
                .setDescription(
                        "The 'Hello Facebook' sample application showcases simple Facebook integration")
                .setLink("http://developers.facebook.com/android");
    }


    private void onSessionStateChange(Session session,Exception exception) {
        if (pendingAction != PendingAction.NONE
                && (exception instanceof FacebookOperationCanceledException || exception instanceof FacebookAuthorizationException)) {
            InfoDialog.newInstance(FacebookReadyBaseActivity.this,"Failed","Not granted.",null).show();
            pendingAction = PendingAction.NONE;
        } else if (state == SessionState.OPENED_TOKEN_UPDATED) {
            handlePendingAction();
        }

    }

}

解决方法

终于搞定了!

在提供的HelloFacebookSample项目中,我在清单中看到了这个:

<provider android:authorities="com.facebook.app.NativeAppCallContentProvider355198514515820"
                  android:name="com.facebook.NativeAppCallContentProvider"
                  android:exported="true"/>

我只是使用自己的Facebook应用程序ID更改了数字部分.不知道为什么我没有在文档中看到这一点.

编辑
之后发现:https://developers.facebook.com/docs/reference/android/current/class/NativeAppCallContentProvider/

原文链接:https://www.f2er.com/android/314156.html

猜你在找的Android相关文章