Sunday, August 14, 2011

Two ways to upload photo to Facebook

Graph and REST

1. Use Graph
/ALBUM_ID/photos
This way needs album id to upload a photo within a album.


Bundle params = new Bundle();
Uri uri = (from somewhere)
byte[] data = null;

InputStream is = mContext.getContentResolver().openInputStream(uri);
data = new byte[is.available()];
is.read(data);

if (data != null) {
    params.putString("message", "test");
    params.putByteArray("source", data);
    String response = Facebook.request(ALBUM_ID/photos, params, "POST");
}


2. Use REST
photos.upload
This doesn't need a album id, instead it creates a album with your application name if there doesn't exist one.


Bundle params = new Bundle();
Uri uri = (from somewhere)
byte[] data = null;

InputStream is = mContext.getContentResolver().openInputStream(uri);
data = new byte[is.available()];
is.read(data);

if (data != null) {
    params.putString("method", "photos.upload");
    params.putByteArray("picture", data);
    String response = Facebook.request(null, params, "POST");
}