1

I want to post image along with text caption via facebook SDK.

I am referring to the solution posted here.

Android post picture to Facebook wall

However, I am getting the following error.

FATAL EXCEPTION: main
java.lang.NullPointerException
    at java.net.URLDecoder.decode(URLDecoder.java:104)
    at java.net.URLDecoder.decode(URLDecoder.java:48)
    at com.facebook.android.Util.openUrl(Util.java:173)
    at com.facebook.android.Facebook.request(Facebook.java:751)
    at com.xxx.yyy.gui.share.ShareDialogFactory.shareThroughFacebook(ShareDialogFactory.java:170)

I had verified my code against the previous posted solution.

private static void shareThroughFacebook(final Context context, final File file, final int heartbeatForShare) {
    final Facebook facebook = new Facebook("285243934881501");

    // Login.
    facebook.authorize((Activity)context, new String[] { "publish_stream" }, new DialogListener() {
        @Override
        public void onComplete(Bundle values) {              
            android.util.Log.i("CHEOK", "onComplete");
        }

        @Override
        public void onFacebookError(FacebookError error) {
            android.util.Log.i("CHEOK", "onFacebookError");
        }

        @Override
        public void onError(DialogError e) {
            android.util.Log.i("CHEOK", "onError");
        }

        @Override
        public void onCancel() {
            android.util.Log.i("CHEOK", "onCancel");
        }
    });         

    android.util.Log.i("CHEOK", "STEP 1");

    final byte[] data = JPEG2ByteArray(file);        
    final Bundle bundle = new Bundle();        
    bundle.putString(Facebook.TOKEN, facebook.getAccessToken());        
    android.util.Log.i("CHEOK", "data length is " + data.length);
    bundle.putByteArray("picture", data);
    bundle.putString("message", "this-is-message");
    bundle.putString("caption", "this-is-caption");

    // I can reach till here!
    android.util.Log.i("CHEOK", "STEP 2");

    try {
        // NPE happens right here!!!
        facebook.request("me/photos", bundle, "POST");
    } catch (FileNotFoundException e1) {
        android.util.Log.i("CHEOK", "FileNotFoundException");
        e1.printStackTrace();
    } catch (MalformedURLException e1) {
        android.util.Log.i("CHEOK", "MalformedURLException");
        e1.printStackTrace();
    } catch (IOException e1) {
        android.util.Log.i("CHEOK", "IOException");
        e1.printStackTrace();
    }

    android.util.Log.i("CHEOK", "STEP 3");
}

I can confirm my byte array is non-zero length. I do not receive any callback during authorize, not sure whether this is correct behaviour. Note, the code shareThroughFacebook is invoked from a Dialog.

Is there any other steps I had missed out?

Community
  • 1
  • 1
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

0 Answers0