I use the Facebook Android SDK.
Create multiple posts in news feed of Facebook logged in user that will contain photo from the Android device (its SD card) and some comment. The result should be the same as when you do it using the Add photo/video feature directly in Facebook. In the end, it should look like this:
I can't do it.
I went through the numerous similar posts on Stack Overflow, but no answer there so far.
Upload pictures from my mobile (its SD card) to an album that is created for my application the first time I upload a picture from it. In this case, when constructing the params object, I use the picture
key and put the bytes of the picture as its value. I use me/photos
in the request(...)
call of the Facebook (or AsyncFacebookRunner) object. **
Not all uploaded images are displayed on my wall. Instead, there is something like x photos were added to the album xxx.
Bundle params = new Bundle();
params.putString("message", "Uploaded on " + now());
params.putByteArray("picture", bytes); //bytes contains photo bytes, no problem here
asyncRunner.request("me/photos", params, "POST", new PostPhotoRequestListener(), null);
Display pictures stored somewhere on the Internet in posts on my wall. In this case, when constructing the params object, I use the link
key and set the url to picture as its value. I use me/feed
in the request(...)
call.
This produces some strange output, but it isn't what I want.
Bundle params = new Bundle();
params.putString("message", "Uploaded on " + now());
params.putString("link", "http://i1114.photobucket.com/albums/k538/tom_rada/bota2.jpg");
asyncRunner.request("me/feed", params, "POST", new PostPhotoRequestListener(), null);
I try to use the picture
key and set photo bytes as its value (as in 1.), and call the request with me/feed
(as in 2.),
Message is produced as I would like it to be, but no photo is included
Bundle params = new Bundle();
params.putString("message", "Uploaded on " + now());
params.putByteArray("picture", bytes); //bytes contains photo bytes, no problem here
asyncRunner.request("me/feed", params, "POST", new PostPhotoRequestListener(), null);
It seems that the only way to create new posts containing photos on user's wall is to add photos and related comments to user's Wall photos album.
Beware: The facebook.request
call should be replaced with async call, so the operation doesn't block the UI thread !!!
String wallAlbumID = null;
String response = facebook.request("me/albums");
JSONObject json = Util.parseJson(response);
JSONArray albums = json.getJSONArray("data");
for (int i =0; i < albums.length(); i++) {
JSONObject album = albums.getJSONObject(i);
if (album.getString("type").equalsIgnoreCase("wall")) {
wallAlbumID = album.getString("id");
Log.d("JSON", wallAlbumID);
break;
}
}
... and then
if (wallAlbumID != null) {
Bundle params = new Bundle();
params.putString("message", "Uploaded on " + now());
params.putByteArray("source", bytes);
asyncRunner.request(wallAlbumID+"/photos", params, "POST", new PostPhotoRequestListener(), null);
}
I can't upload photos on Facebook Make sure that you have a strong Wi-Fi or network connection. Try uploading the original photo instead of an edited version. Check the size of the photo. We recommend uploading photos under 15MB.
Go to your Android's settings and open your application manager. Tap Facebook. Tap Move to SD card.
Facebook facebook = new Facebook("your appid");
private void uploadImage()
{
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] data = stream.toByteArray();
facebook.authorize(FacebookActivity.this, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"},new DialogListener()
{
@Override
public void onComplete(Bundle values)
{
//uploadImageOnlyToWall(data, "Uploading Image only to wall","Test Post from Android while uploading photo with message");
uploadImageToWallAndAlbums(imageUrl, "Image via link");
}
@Override
public void onFacebookError(FacebookError error)
{
Toast.makeText(FacebookActivity.this, "FaceBook Error", Toast.LENGTH_LONG).show();
}
@Override
public void onError(DialogError e)
{
Toast.makeText(FacebookActivity.this, "Error", Toast.LENGTH_LONG).show();
}
@Override
public void onCancel()
{
Toast.makeText(FacebookActivity.this, "Canceled", Toast.LENGTH_LONG).show();
}
});
}
private void uploadImageOnlyToAlbum(byte[] byteArray,String caption)
{
Bundle params = new Bundle();
params.putByteArray("picture", byteArray);
params.putString("caption",caption);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me/photos", params, "POST", new SampleUploadListener(), null);
}
private void uploadImageToWallAndAlbums(byte[] byteArray,String caption)
{
Bundle params = new Bundle();
params.putString("method", "photos.upload");
params.putByteArray("picture", byteArray);
params.putString("caption", caption);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With