There isn't seem to be a clear reference about this. I'm creating an Android app which user can login to FB.
I followed this tutorial on FB site, which gives an example of publishing a picture from a web URL: postParams.putString("picture", "https:// image URL");
However, i want to upload to the logged-in user's timeline a local PNG image from my project, which located on all res-drawable folders.
Here is my code:
void publishStory()
{
Session session = Session.getActiveSession();
if (session != null)
{
Bundle postParams = new Bundle();
postParams.putString("name", "Name here.");
postParams.putString("caption", "Caption here.");
postParams.putString("description", "Description here.");
postParams.putString("link", "https://developers.facebook.com/android");
byte[] data = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bi = BitmapFactory.decodeResource(getResources(),R.drawable.logonew);
bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
data = baos.toByteArray();
postParams.putString("method", "photos.upload");
postParams.putByteArray("picture", data);
Request.Callback callback = new Request.Callback()
{
public void onCompleted(Response response)
{
FacebookRequestError error = response.getError();
if (error != null)
Toast.makeText(_context , error.getErrorMessage(), Toast.LENGTH_SHORT).show();
else
Toast.makeText(_context, "Posted successful on your wall", Toast.LENGTH_SHORT).show();
}
};
Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}
All the examples i could find are dealing with Facebook class instances and AsyncFacebookRunner which are depressed.
Moreover, the error response i get from the request is: HttpStatus: 400, errorCode: 100, errorType: GraphMethodException, errorMessage: Unsupported method, photos.upload
So what is the photos.upload replacement? Please advise, a code example will be great, tnx.
If you want to upload a photo, why not just use the newUploadPhotoRequest in the Reqeust class? https://developers.facebook.com/docs/reference/android/3.0/Request#newUploadPhotoRequest%28Session,%20Bitmap,%20Callback%29
Ming Li got me onto the right track but here's a more complete solution. This is tested and working. There are two elements: (1) create callback to get the URL of the uploaded photo; (2) code to upload the photo. Here's the complete code with both parts. The URL of the photo will be loaded into the string variable
fbPhotoAddress
.