Android: unable to upload image to facebook album

277 views Asked by At

I was writing the code for uploading image to facebook album "Profile Images", after selecting the image from gallery it is converted to byte array imageBytes and send to the AsyncTask. I use the fallowing code to upload the image. facebookProfileImagesAlbumId has the Id for the album "Profile Images"

 Bundle params_ = new Bundle();
 String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);

 params_.putString("source", encodedImage);
 /* make the API call */
 new GraphRequest(
        AccessToken.getCurrentAccessToken(),
        "/"+facebookProfileImagesAlbumId+"/photos",
        params_,
        HttpMethod.POST,
        new GraphRequest.Callback() {
              public void onCompleted(GraphResponse response) {
                   Log.i("Login", response.toString());
              }
        }).executeAndWait();

but it is not working and Log.i("Login", response.toString()); gives the following message

{Response: responseCode: 200, graphObject: null, error: {HttpStatus: -1, errorCode: -1, errorType: null, errorMessage: could not construct request body}}

I am using Facebook Graph API 2.4

1

There are 1 answers

3
Rajesh Jadav On BEST ANSWER

I have used following code to Post image to facebook wall or specific album.

public void PostImage(Bitmap bitmap) {
        AccessToken token = AccessToken.getCurrentAccessToken();
        if (token != null) {
            Bundle parameters = new Bundle(2);
            parameters.putParcelable("source", bitmap);
            parameters.putString("message", "description");
            new GraphRequest(token, "/"+facebookProfileImagesAlbumId+"/photos", parameters, HttpMethod.POST, new GraphRequest.Callback() {

                public void onCompleted(GraphResponse response) {
                    Log.e("Image Post", "Res =" + response.toString());
                }
            }).executeAsync();
        }
    }

Hope this helps!