Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android post picture to Facebook wall

Tags:

I am trying to post a picture to my wall on Facebook. I have managed logging in and posting text to the wall. However, when I try posting the picture, nothing happens.

I am using the Android Facebook SDK.

Here is what I have so far:

    Bundle params = new Bundle();
    params.putString("method", "photos.upload");

    Toast.makeText(FacebookPoster.this, "" + getIntent().getExtras().getByteArray("data").length, Toast.LENGTH_SHORT).show();

    params.putByteArray("picture", getIntent().getExtras().getByteArray("data"));

    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
    mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);

The Toast shows 8733, which means the byte array isn't empty

NB. Logcat output some warnings (not errors):

03-02 14:19:29.554: WARN/Bundle(1891): Attempt to cast generated internal exception:
03-02 14:19:29.554: WARN/Bundle(1891): java.lang.ClassCastException: java.lang.String
03-02 14:19:29.554: WARN/Bundle(1891):     at android.os.Bundle.getByteArray(Bundle.java:1305)
03-02 14:19:29.554: WARN/Bundle(1891):     at com.facebook.android.Util.openUrl(Util.java:155)
03-02 14:19:29.554: WARN/Bundle(1891):     at com.facebook.android.Facebook.request(Facebook.java:559)
03-02 14:19:29.554: WARN/Bundle(1891):     at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:253)
03-02 14:19:29.584: WARN/Bundle(1891): Key method expected byte[] but value was a java.lang.String.  The default value <null> was returned.

(Shows several times underneath each other.)

What am I doing wrong?


SOLVED. This is what I did to make it work:

facebook.authorize(this, new String[] { "publish_stream" },
        new DialogListener() {

    @Override
    public void onFacebookError(FacebookError e) {
    // TODO Auto-generated method stub
    }

    @Override
    public void onError(DialogError dialogError) {
    // TODO Auto-generated method stub
    }

    @Override
    public void onComplete(Bundle values) {
        postToWall(values.getString(Facebook.TOKEN));                                              
    }

    @Override
    public void onCancel() {
    // TODO Auto-generated method stub
    }
});

And the helper method:

private void postToWall(String accessToken) {       
    Bundle params = new Bundle();

    params.putString(Facebook.TOKEN, accessToken);

    // The byte array is the data of a picture.
    params.putByteArray("picture", getIntent().getExtras().getByteArray("data"));

    try {
        facebook.request("me/photos", params, "POST");

    } catch (FileNotFoundException fileNotFoundException) {
        makeToast(fileNotFoundException.getMessage());
    } catch (MalformedURLException malformedURLException) {
        makeToast(malformedURLException.getMessage());
    } catch (IOException ioException) {
        makeToast(ioException.getMessage());
    }
}
like image 366
whirlwin Avatar asked Mar 02 '11 13:03

whirlwin


People also ask

Why can't I post pictures on Facebook from my phone?

You can try removing the cache files of the Facebook app and see if that resolves the can t upload pictures to Facebook problem. Go to Settings > Apps & notifications > Facebook > Storage & cache on your device. Tap Clear storage and then Clear cache. Open the Facebook app and see if you can upload a photo.


2 Answers

first thing is that you are not using graph api to upload the pictures... u r using the old rest api... try to use graph api, its simple...

Use following code:

Bundle param = new Bundle();
param.putString("message", "picture caption");
param.putByteArray("picture", ImageBytes);
mAsyncRunner.request("me/photos", param, "POST", new SampleUploadListener());

According to error message, it looks like its giving errors in getting bytes from intent's bundle...

like image 167
Farhan Avatar answered Oct 30 '22 00:10

Farhan


btnLogin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                facebook.authorize(FbdemoActivity.this, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"},new DialogListener() {
                    @Override
                    public void onComplete(Bundle values) {
                    }

                    @Override
                    public void onFacebookError(FacebookError error) {
                    }

                    @Override
                    public void onError(DialogError e) {
                    }

                    @Override
                    public void onCancel() {
                    }
                });

            }
        });

public void postImageonWall() {

           byte[] data = null;

             Bitmap bi = BitmapFactory.decodeFile("/sdcard/viewitems.png");
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
             data = baos.toByteArray();


             Bundle params = new Bundle();
             params.putString(Facebook.TOKEN, facebook.getAccessToken());
             params.putString("method", "photos.upload");
             params.putByteArray("picture", data);

             AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
             mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);

    }

public class SampleUploadListener extends BaseRequestListener {

    public void onComplete(final String response, final Object state) {
        try {
            // process the response here: (executed in background thread)
            Log.d("Facebook-Example", "Response: " + response.toString());
            JSONObject json = Util.parseJson(response);
            final String src = json.getString("src");

            // then post the processed result back to the UI thread
            // if we do not do this, an runtime exception will be generated
            // e.g. "CalledFromWrongThreadException: Only the original
            // thread that created a view hierarchy can touch its views."

        } catch (JSONException e) {
            Log.w("Facebook-Example", "JSON Error in response");
        } catch (FacebookError e) {
            Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
        }
    }

    @Override
    public void onFacebookError(FacebookError e, Object state) {    
    }
}

try this code it will work i had used the same code and uploads the image on Facebook.

like image 42
Nilesh Gawade Avatar answered Oct 30 '22 00:10

Nilesh Gawade