5

I would like to know if it's possible and if so how I can transmit the the raw image data instead of specifying the URL to an image on the web when posting to my Facebook wall?

The working code I currently have results in this:

http://1.bp.blogspot.com/-klSLFEmvHy0/TxpPMIay0xI/AAAAAAAAAKQ/iLZv-QtmThc/s1600/fbpost.JPG (sorry I don't have enough credits to embed pictures so open the URL please :-)

Which is moreorless what I want with the caption, title, links etc. but my ultimate objective is to make the heart-shaped image displayed above be the outputted one that my app generates each time a user decides to post their results to Facebook (the contents are dynamically generated on the image). I don't want to implement uploading some third party server and then sharing that URL.

I've seen around people referring to code like this:

...
    byte[] data = null;
    Bitmap bi = BitmapFactory.decodeFile(imagePath);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    data = baos.toByteArray();
    params.putByteArray(...

but that seems to be for uploading photos directly to an album which isn't what I want either.

My working code so far is:

private void loginAndPostToFacebook() {
    mFacebook = new Facebook(FACEBOOK_APP_ID);

    mFacebook.authorize(this, new String[] { "publish_stream" }, new DialogListener() {
        @Override
        public void onComplete(Bundle values) {
            postOnWall();
        }

        @Override
        public void onFacebookError(FacebookError error) {
            L.t(getString(R.string.error_facebook_error) + ": " + error.getMessage());
        }

        @Override
        public void onError(DialogError e) {
            L.t(getString(R.string.error_facebook_error) + ": " + e.getMessage());
        }

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

public void postOnWall() {
    Bundle params = new Bundle();

    // Message
    params.putString("message", "my msg");

    // Name
    params.putString("name", "app name");

    // Caption
    params.putString("caption", "caption");

    // Description
    params.putString("description", "description");

    // Here's where I'd insert my dynamically generated image data...       
    params.putString("picture", "http://4.bp.blogspot.com/-VaNzm3xMOtk/TxpKzhxpdEI/AAAAAAAAAKI/08Kc5b4HW0Q/s1600/sexometer128x128neutral_icon.png");        

    // Link
    params.putString("link", "http://www.stackoverflow.com");

    try {
        String response = mFacebook.request("me");
        response = mFacebook.request("me/feed", params, "POST");
        if (response == null || response.equals("") || response.equals("false")) {
            L.t(getString(R.string.error_facebook_error) + ": blank response");
            return;
        } else if (response.toLowerCase().contains("error")) {
            L.t(getString(R.string.error_facebook_error) + ": " + response);
            return;
        }
    } catch(Exception e) {
        L.t(getString(R.string.error_facebook_error) + ": " + e.getMessage());
        return;
    }

    L.t(getString(R.string.success_posted_to_facebook));
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (mFacebook != null) {
        mFacebook.authorizeCallback(requestCode, resultCode, data);
    }
}
Kara
  • 6,115
  • 16
  • 50
  • 57
pilcrowpipe
  • 2,528
  • 6
  • 29
  • 41

1 Answers1

0

From my understanding, you will need to upload the photo to an App's album first. It will automatically post it to the Wall if the "no_story" parameter is false.

https://developers.facebook.com/docs/reference/api/user/#photos

Alexcode
  • 1,598
  • 7
  • 15