Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Facebook SDK 3.0 upload local image

There isn't seem to be a clear reference about this. I'm creating an Android app which user can login to FB.

I followed this tutorial on FB site, which gives an example of publishing a picture from a web URL: postParams.putString("picture", "https:// image URL");

However, i want to upload to the logged-in user's timeline a local PNG image from my project, which located on all res-drawable folders.

Here is my code:

void publishStory() 
{
        Session session = Session.getActiveSession();

        if (session != null)
        {       
            Bundle postParams = new Bundle();

            postParams.putString("name", "Name here.");
            postParams.putString("caption", "Caption here.");
            postParams.putString("description", "Description here.");
            postParams.putString("link", "https://developers.facebook.com/android");

            byte[] data = null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Bitmap bi = BitmapFactory.decodeResource(getResources(),R.drawable.logonew);
            bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
            data = baos.toByteArray();

            postParams.putString("method", "photos.upload");
            postParams.putByteArray("picture", data);

            Request.Callback callback = new Request.Callback() 
            {
                public void onCompleted(Response response) 
                {
                    FacebookRequestError error = response.getError();

                    if (error != null) 
                        Toast.makeText(_context , error.getErrorMessage(), Toast.LENGTH_SHORT).show();

                    else 
                        Toast.makeText(_context, "Posted successful on your wall", Toast.LENGTH_SHORT).show();
                }
            };

            Request request = new Request(session, "me/feed", postParams, HttpMethod.POST, callback);
            RequestAsyncTask task = new RequestAsyncTask(request);
            task.execute();
        }
    }

All the examples i could find are dealing with Facebook class instances and AsyncFacebookRunner which are depressed.

Moreover, the error response i get from the request is: HttpStatus: 400, errorCode: 100, errorType: GraphMethodException, errorMessage: Unsupported method, photos.upload

So what is the photos.upload replacement? Please advise, a code example will be great, tnx.

like image 483
Jigsaw2300 Avatar asked Jun 02 '13 15:06

Jigsaw2300


2 Answers

If you want to upload a photo, why not just use the newUploadPhotoRequest in the Reqeust class? https://developers.facebook.com/docs/reference/android/3.0/Request#newUploadPhotoRequest%28Session,%20Bitmap,%20Callback%29

Bitmap image = ... // get your bitmap somehow
Request.Callback callback = ... // create your callback
Request request = Request.newUploadPhotoRequest(session, image, callback);
request.executeAsync();
like image 72
Ming Li Avatar answered Sep 27 '22 17:09

Ming Li


Ming Li got me onto the right track but here's a more complete solution. This is tested and working. There are two elements: (1) create callback to get the URL of the uploaded photo; (2) code to upload the photo. Here's the complete code with both parts. The URL of the photo will be loaded into the string variable fbPhotoAddress.

   String fbPhotoAddress = null;

 // Part 1: create callback to get URL of uploaded photo
    Request.Callback uploadPhotoRequestCallback = new Request.Callback() {
    @Override
       public void onCompleted(Response response) {
    // safety check
          if (isFinishing()) {
            return;
        }
        if (response.getError() != null) {  // [IF Failed Posting]
           Log.d(logtag, "photo upload problem. Error="+response.getError() );
        }  //  [ENDIF Failed Posting]

        Object graphResponse = response.getGraphObject().getProperty("id");
        if (graphResponse == null || !(graphResponse instanceof String) || 
            TextUtils.isEmpty((String) graphResponse)) { // [IF Failed upload/no results]
               Log.d(logtag, "failed photo upload/no response");
        } else {  // [ELSEIF successful upload]
            fbPhotoAddress = "https://www.facebook.com/photo.php?fbid=" +graphResponse;                             
        }  // [ENDIF successful posting or not]
     }  // [END onCompleted]
  }; 

//Part 2: upload the photo
  Request request = Request.newUploadPhotoRequest(session, imageSelected, uploadPhotoRequestCallback);
  request.executeAsync();
like image 25
PeteH Avatar answered Sep 27 '22 18:09

PeteH