Facebook SDK 4.0 分享对话框

Facebook SDK 4.0 ShareDialog

我想使用 Facebook SDK 4.0 从我的 Android 应用中分享一张图片。我让它与 ShareDialog 一起工作,但是当用户没有安装 FB 应用程序时,根据 developers.facebook,SDK 应该使用 Web 共享对话框:

In past versions of the SDK for Android, your app had to check for a native, installed Facebook app before it could open the Share Dialog. If the person didn't have the app installed, you had to provide your own code to call a fallback dialog.

Now the SDK automatically checks for the native Facebook app. If it isn't installed the Web Share dialog launches:

但是当我删除 FB 应用程序并尝试分享时没有任何反应。
这是我的代码:

            ShareDialog shareDialog = new ShareDialog(this);

            BitmapFactory.Options bmOptions = new BitmapFactory.Options();

            bmOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;

            Bitmap image = BitmapFactory.decodeFile(imagePath, bmOptions);

            SharePhoto photo = new SharePhoto.Builder().setBitmap(image).build();

            SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(photo).build();

            shareDialog.show(content, ShareDialog.Mode.AUTOMATIC);

编辑

有没有办法在没有安装 facebook 应用程序的情况下在 facebook 上分享照片?

请仔细阅读SDK文档。 没有安装 Facebook 应用程序,您无法使用 sharedialog 分享照片。

参考:https://developers.facebook.com/docs/sharing/android

Photos

People can share photos from your app to Facebook with the Share Dialog or with a custom interface.

The photos must be less than 12MB in size

People need the native Facebook for Android app installed, version 7.0 or higher

我找到了解决方案:

BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;

    Bitmap image = BitmapFactory.decodeFile(imagePathForShare, bmOptions);

    SharePhoto photo = new SharePhoto.Builder().setBitmap(image).build();

    SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(photo).build();

    Toast.makeText(getApplicationContext(), getString(R.string.facebook_uploading), Toast.LENGTH_SHORT).show();

    ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
        @Override
        public void onSuccess(Sharer.Result result)
        {
            Toast.makeText(getApplicationContext(), getString(R.string.facebookSuccessful), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel()
        {
            Log.v("FACEBOOK_TEST", "share api cancel");
        }

        @Override
        public void onError(FacebookException e)
        {
            Log.v("FACEBOOK_TEST", "share api error " + e);
        }
    });

试试这个:

我遇到了同样的麻烦,我可以说是 facebook 的 SharePhotoContent API 中存在一些问题。您可以使用 ShareLinkContent 分享图片。

ShareLinkContent linkContent = new ShareLinkContent.Builder()
                    .setImageUrl("Your url")
                    .build();
shareDialog.show(linkContent);

You can use share dialog to share photos.

试试这个

 Bitmap image = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                    SharePhoto.Builder photoBuilder = new SharePhoto.Builder();
                    photoBuilder.setBitmap(image);
                    photoBuilder.setCaption("HBD Caption");
                    final SharePhoto sharePhoto = photoBuilder.build();
                    SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(sharePhoto).build();
                    mShareDialog.show(content);

To add caption, you'll need to add publish permissions from facebook. If you wont do this, your image will get posted without a caption.

参考:https://developers.facebook.com/docs/graph-api/reference/user/permissions/