Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to upload photo from the SD card to the Facebook wall

I use the Facebook Android SDK.

Goal

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:

Wanted Facebook result

Goal

Problem

I can't do it.

I went through the numerous similar posts on Stack Overflow, but no answer there so far.

What I have tried to implement so far

Approach 1: SD card photos 2 Facebook album

How

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. **

The problem

Not all uploaded images are displayed on my wall. Instead, there is something like x photos were added to the album xxx.

The code snippet is this (for one picture)

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);

Facebook result

Photos to album

Approach 2: Internet photos 2 facebook news feed

How

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.

The problem

This produces some strange output, but it isn't what I want.

The code snippet is this (for one picture)

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);

Facebook result

Linked photos

Approach 3: Mix of approach 1 and 2

How

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.),

The problem

Message is produced as I would like it to be, but no photo is included

The code snippet is this (for one picture)

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);

Facebook result

Almost there

So, any ideas how I could reach my goal?

EDIT - WORKAROUND FOUND

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.

How - Code snippet

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);
}
like image 421
Tomas Avatar asked Dec 28 '11 23:12

Tomas


People also ask

Why can't I post pictures from my gallery on Facebook?

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.

How do I change Facebook storage to SD card?

Go to your Android's settings and open your application manager. Tap Facebook. Tap Move to SD card.


1 Answers

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);            
            }
like image 109
KK_07k11A0585 Avatar answered Nov 15 '22 19:11

KK_07k11A0585