使用SDK将照片/图像上传到facebook

时间:2013-01-04 08:34:49

标签: android facebook image upload

在Facebook上发送照片/图像/位图的最新方法是什么?我发现所有方法都已弃用。如果是从点击按钮的那一刻起最好的。

我知道有些人在方法中使用它,在哪里插入它?:

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

(抱歉英语:P)

2 个答案:

答案 0 :(得分:1)

您可以使用RestFB使用Java向Facebook发出请求。您可以上传以下照片:

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

答案 1 :(得分:1)

首先下载this Facebbok SDK并将其作为库项目包含在您的项目中。

在您的活动中,您想要分享图片::

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

在onCreate()中初始化这个变量::

 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,您将从那里分享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();
        }
}