正在使用 Graph 将照片上传到 Facebook API

Uploading photo to Facebook using Graph API

我正在开发一个简单的 android 应用程序 post 到 facebook 和 twitter。 到目前为止,我已经成功 posting 了简单的状态更新和推文。 现在我需要 post 一张照片和状态。我是 Android 开发新手,不知道如何上传照片。

这是我用来更新 Facebook 状态的代码。有人可以修改它以从 ImageView 上传照片吗?

public void postToFacebook(){

    final EditText message = (EditText) findViewById(R.id.message);

    if (isFacebookAuthed()){

        String path = "me/feed";
        AccessToken at = AccessToken.getCurrentAccessToken();
        Bundle parameters = new Bundle();
        parameters.putString("message", message.getText().toString());
        HttpMethod method = HttpMethod.POST;
        GraphRequest.Callback cb = new GraphRequest.Callback() {

            @Override
            public void onCompleted(GraphResponse graphResponse) {

                //check graphResponse for success or failure
                if(graphResponse.getError()==null){
                    Toast.makeText(Home.this, "Successfully posted to Facebook", Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(Home.this, "Facebook: There was an error, Please Try Again", Toast.LENGTH_SHORT).show();

                }
            }
        };

        GraphRequest request = new GraphRequest(at,path,parameters,method,cb);
        request.setParameters(parameters);
        request.executeAsync();


    }
    else
    {

        Toast.makeText(Home.this,"You are not logged into Facebook", Toast.LENGTH_SHORT).show();
    }

}

注意:isFacebookAuhed() 是一个布尔方法,returns如果用户已登录 facebook,则为真。

您可以使用以下参数在 Facebook 上 post 照片。

您需要将图像转换为字节数组。

parameters.putByteArray("picture", bytearray);

SharePhotoContent怎么样?

 Bitmap bitmap = BitmapFactory.decodeFile(path); // From file or memory
 SharePhoto photo = new SharePhoto.Builder().setBitmap(bitmap).build();
 SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(photo).build();
 ShareDialog dialog = new ShareDialog(this);   // this is current activity
 dialog.show(content);

当然,在调用 ShareDialog.show().

之前,您需要获得 logInWithPublishPermissions"publish_actions" 的授权
 public void postToFacebook(){

        final EditText message = (EditText) findViewById(R.id.editText);

        if(AccessToken.getCurrentAccessToken() != null){
         /*   Bitmap image=BitmapFactory.decodeResource(getResources(),R.drawable.ll);
             ByteArrayOutputStream blob1=new ByteArrayOutputStream();
                     image.compress(Bitmap.CompressFormat.JPEG,0,blob1);
            byte[] bitmapdata = blob1.toByteArray();*/
            Bitmap image=BitmapFactory.decodeResource(getResources(),R.drawable.ll);
            ByteArrayOutputStream blob=new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.JPEG,0,blob);
            byte[] bitmapdata=blob.toByteArray();
            String path = "me/feed";
            AccessToken at = AccessToken.getCurrentAccessToken();
            Bundle parameters = new Bundle();
           parameters.putByteArray("image",bitmapdata);
            parameters.putString("message", message.getText().toString());
            HttpMethod method = HttpMethod.POST;
            GraphRequest.Callback cb = new GraphRequest.Callback() {

                @Override
                public void onCompleted(GraphResponse graphResponse) {

                    //check graphResponse for success or failure
                    if(graphResponse.getError()==null){
                        Toast.makeText(AndroidFacebookConnectActivity.this, "Successfully posted to Facebook", Toast.LENGTH_SHORT).show();
                    }
                    else{
                        Toast.makeText(AndroidFacebookConnectActivity.this, "Facebook: There was an error, Please Try Again", Toast.LENGTH_SHORT).show();

                    }
                }
            };

            GraphRequest request = new GraphRequest(at,path,parameters,method,cb);
            request.setParameters(parameters);
            request.executeAsync();


        }
        else
        {

            Toast.makeText(AndroidFacebookConnectActivity.this,"You are not logged into Facebook", Toast.LENGTH_SHORT).show();
        }

    }
Bundle params = new Bundle();
                   // params.putString("multipart/form-data", imgurl);
                    params.putByteArray("multipart/form-data",byteArray);

                    params.putString("caption", txtcaption.getText().toString());
                    /* make the API call */
                    new GraphRequest(
                            AccessToken.getCurrentAccessToken(),
                            "/me/photos",
                            params,
                            HttpMethod.POST,
                            new GraphRequest.Callback() {
                                public void onCompleted(GraphResponse response) {
                                    /* handle the result */
                                    Log.e("responseImagedata---", response.toString());

                                }
                            }`enter code here`
                    ).executeAsync();