Maury
Maury

Reputation: 604

Upload a photo/image to facebook with SDK

What is the latest way to send a photo / image / bitmap on the wall facebook? All methods are deprecated that I found. Best if it was from the moment you click on the button.

I know that some people use it in the method, and where to insert it?:

 Request request = Request.newUploadPhotoRequest(
            session, bitmap, callback);
    RequestAsyncTask task = new RequestAsyncTask(request);
    task.execute();

(sorry for english :P)

Upvotes: 0

Views: 1953

Answers (2)

AndroidLearner
AndroidLearner

Reputation: 4636

First Download this Facebbok SDK and include it in your project as library project.

In your activity,where you want to share the image ::

private static Bitmap b;
private static Facebook mFacebook;
private static AsyncFacebookRunner mAsyncRunner;
String[] permissions = {"photo_upload" };
final static int AUTHORIZE_ACTIVITY_RESULT_CODE = 0;

In onCreate() initialize this variables ::

 if (Constant.FACEBOOK_APP_ID == null)
 {
        Util.showAlert(this, "Warning", "Facebook Applicaton ID must be "
                        + "specified before running this example: see FbAPIs.java");
        return;
 }

mHandler=new Handler();
mFacebook = new Facebook(Constant.FACEBOOK_APP_ID);
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
SessionStore.restore(mFacebook, this);


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    switch (requestCode) 
    {
        case AUTHORIZE_ACTIVITY_RESULT_CODE: 
        {
        mFacebook.authorizeCallback(requestCode, resultCode, data);
        break;
        }
    }
}

onButtonClick from where you are going to share a Image ::

if (!mFacebook.isSessionValid()) 
{
    mFacebook.authorize(TabNoteActivity.this, permissions, AUTHORIZE_ACTIVITY_RESULT_CODE, new LoginDialogListener());
}
else
{
     Bundle params = new Bundle();
     ByteArrayOutputStream stream = new ByteArrayOutputStream();
     Utils.b.compress(Bitmap.CompressFormat.PNG, 100, stream);//Here I am convert the JPG image into bitmap using declaring method in the Utils class 
     byte[] byteArray = stream.toByteArray();
     params.putString (Facebook.TOKEN, mFacebook.getAccessToken());
     params.putString ("message"," Facebook App Testing");
     params.putByteArray("picture",byteArray);
     mAsyncRunner.request("me/photos", params,"POST", new PhotoUploadListener(),null);
}


    public class PhotoUploadListener extends BaseRequestListener
    {
        //@Override
        public void onComplete(final String response, final Object state) 
        {

           mHandler.post(new Runnable() 
           {
            // @Override
            public void run() 
            {
                Toast.makeText(Activity.this,"Image has been shared Successfully",Toast.LENGTH_LONG).show();
            }
          });
        }

        public void onFacebookError(FacebookError error) 
        {
            Toast.makeText(getApplicationContext(), "Facebook Error: " + error.getMessage(),Toast.LENGTH_LONG).show();
        }
}

Upvotes: 1

Pulkit Goyal
Pulkit Goyal

Reputation: 5674

You could use RestFB for making requests to Facebook using Java. You can upload photos like this:

FacebookType publishMessageResponse = facebookClient.publish("me/photos", FacebookType.class,
    BinaryAttachment.with("photo.png", new FileInputStream(selectedImagePath)),
    );

Upvotes: 1

Related Questions