Christopher Francisco
Christopher Francisco

Reputation: 16278

Graph Story Image Upload

I'm working on an Android app which lets you post a story and I want the user to upload a photo.

The app has the "publish_actions" permission and it has "user_generated", "fb:explicitly_shared".

My code is the following

ByteArrayOutputStream stream = new ByteArrayOutputStream();
    if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)) {
        Log.i(TAG,"Bitmap compressed into stream successfully.");
    }

    postParams.putByteArray("image", stream.toByteArray());

    postParams.putString("user_generated", "true");
    postParams.putString(OBJECT,URL);
    postParams.putString("fb:explicitly_shared", "true");

Log.i(TAG,"Executing facebook graph story request...");

    Request request = new Request(session, "me/<appnamespace>:<action>", postParams, HttpMethod.POST, callback);
    RequestAsyncTask task = new RequestAsyncTask(request);
    task.execute();

When I check the test account timeline the story is posted, but there is no image. I suppose the problem is on this line:

postParams.putByteArray("image", stream.toByteArray());

I think the problem is either way the key is not "image" or that's not how you do it.

What's the right way to do this?

Upvotes: 0

Views: 996

Answers (1)

Ming Li
Ming Li

Reputation: 15662

From my comment in your other question:

You can't upload an image to your me/: endpoint. You need to POST it to me/staging_resources first. See this how-to on using the Object API (specifically step 3 for image uploading).

https://developers.facebook.com/docs/howtos/androidsdk/3.0/share-using-object-api/

To add the image to your open graph action, do something like:

JSONObject obj = new JSONObject();
obj.put("url", <your staging uri here>);
obj.put("user_generated", true);
myAction.setImage(Arrays.asList(obj));

Upvotes: 1

Related Questions