2

I'm trying to share image via Facebook share dialog. Here's code:

private void startFacebookShare() {
        SharePhoto photo = new SharePhoto.Builder()
                .setBitmap(getCapturedImage())
                .build();
        SharePhotoContent content = new SharePhotoContent.Builder()
                .addPhoto(photo)
                .build();

        ShareDialog shareDialog = new ShareDialog(this);
        shareDialog.registerCallback(fbManager, new FacebookCallback<Sharer.Result>() {
            @Override
            public void onSuccess(Sharer.Result result) {
                Toast.makeText(MainActivity.this, "SUCCESS", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel() {
                Toast.makeText(MainActivity.this, "ONCANCEL"), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(FacebookException e) {
                Toast.makeText(MainActivity.this, "ERROR", Toast.LENGTH_SHORT).show();
            }
        });

        if(isFacebookInstalled()){
            shareDialog.show(content, ShareDialog.Mode.NATIVE);
        }else{
            shareDialog.show(content, ShareDialog.Mode.WEB);
        }
    }

But there is a problem. It works only if: API till 6.0 not include and if Facebook app installed on the device. In the other cases it opens and immediately closes with erro in callback.

How to fix it?

Thanks)

ADD1 (LogCat):

10-05 08:42:43.790 2220-2220/com.industi.polmak_app W/System.err: {FacebookGraphResponseException: An active access token must be used to query information about the current user. httpResponseCode: 400, facebookErrorCode: 2500, facebookErrorType: OAuthException, message: An active access token must be used to query information about the current user.}
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at com.facebook.internal.NativeProtocol.getExceptionFromErrorData(NativeProtocol.java:788)
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at com.facebook.share.internal.ShareInternalUtility.handleActivityResult(ShareInternalUtility.java:166)
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at com.facebook.share.internal.ShareInternalUtility$3.onActivityResult(ShareInternalUtility.java:258)
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at com.facebook.internal.CallbackManagerImpl.onActivityResult(CallbackManagerImpl.java:82)
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at com.industi.polmak_app.activities.MainActivity.onActivityResult(MainActivity.java:125)
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:5423)
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at android.app.ActivityThread.access$1300(ActivityThread.java:135)
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at android.os.Looper.loop(Looper.java:136)
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5017)
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-05 08:42:43.850 2220-2220/com.industi.polmak_app W/System.err:     at dalvik.system.NativeStart.main(Native Method)
Alex Boyko
  • 97
  • 2
  • 9
  • Post your LogCat. – Sufian Oct 05 '16 at 10:03
  • @Sufian if no Facebook app or API 6.0+ - in FacebookCallback calls onError method and R.string.facebook_must_be_installed prints. in other situations everything works fine and opens native Facebook share dialog with my picture – Alex Boyko Oct 05 '16 at 10:11
  • @Sufian if no Facebook app or API 6.0+ share dialog opens for a moment and immidiately closes and calls onError – Alex Boyko Oct 05 '16 at 10:13
  • If you could update your question with the LogCat, it would be helpful. – Sufian Oct 05 '16 at 10:13
  • @Sufian edited my question. nothing in LogCat, just shows toast "ERROR" – Alex Boyko Oct 05 '16 at 11:48
  • Weird. Do following in your LogCat window/tab: **1.** switch to "warn", **2.** "show only selected applications", **3.** select your device and package name, **4.** clean LogCat before running the action (which leads to the crash) is taken. – Sufian Oct 05 '16 at 11:51
  • @Sufian the app doesn't crash. I make picture with camera and then started sharing taken picture. Share dialog even starts and has text "Loading..." but closes after about 1 sec. app still working after that. just closes facebook share dialog. and in LogCat no messages from facebook. just from other components of the app – Alex Boyko Oct 05 '16 at 12:07
  • okay, then you'll have to see what error it is. Do this in your `onError()` - `Log.e("FbError", e.toString());` and post it here. – Sufian Oct 05 '16 at 12:14
  • @Sufian oh god, I'm so stupid. added. sorry) – Alex Boyko Oct 05 '16 at 12:44
  • The user's session is invalid. See this similar question: http://stackoverflow.com/questions/14806522/an-active-access-token-must-be-used-to-query-information-about-the-current-user – Sufian Oct 05 '16 at 14:47

1 Answers1

2

The above can happen if you are trying to use facebook sdk from a facebook account which is not an administrator or an developer account for the project you are working on. You need to go to the facebook developers page and add the facebook account you are trying to access your sdk from.

There is also one answer which has received acceptance for the same type of issue, if the above doesnt work then you can try this:

Facebook android shareDialog closes after opening

Community
  • 1
  • 1
Anand Sonawane
  • 146
  • 1
  • 16