Facebook’s example code for Android shows how to upload a photo using the REST API rather than the newer Graph API. I knew that the iOS SDK can handle it via Graph just fine, so I changed the code in my Android app to use Graph as well (also since doing it with REST wasn’t working). I’ve added a quick method for uploading a picture below, hopefully it works since it was modified from the original and thus untested. It runs synchronously, so you will want to run this in a separate thread (or else convert it to the Async API – in my app it was just easier to make it multithreaded).
public static String uploadPhoto(Facebook facebook, Bitmap image,
String albumId)
{
if ( albumId == null ) {
albumId = "me";
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
image.compress(CompressFormat.JPEG, 75, bos);
byte[] photoBytes = bos.toByteArray();
Bundle params = new Bundle();
params.putByteArray("picture", photoBytes);
try {
String resp = facebook.request(albumId + "/photos", params, "POST");
JSONObject json = Util.parseJson(resp);
return json.getString("id");
} catch ( IOException e ) {
} catch ( FacebookError e ) {
} catch ( JSONException e ) {
}
return null;
}
It is not working for my, I am trying with different codes but I can´t, could you send me a little project with the minimal classes required to upload a picture to facebook?
Thanks in advance,
Andrés.
How to implement Uploading a Photo with the flicker Android SDK….