使用Graph API将照片上传到Facebook

时间:2015-05-05 05:38:23

标签: java android facebook facebook-graph-api android-studio

我正在开发一个简单的Android应用程序发布到Facebook和Twitter。 到目前为止,我已成功发布简单的状态更新和推文。 现在我需要发布一张照片和状态。我是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()是一个布尔方法,如果用户登录到facebook,则返回true。

3 个答案:

答案 0 :(得分:1)

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

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

parameters.putByteArray("picture", bytearray);

答案 1 :(得分:0)

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();

答案 2 :(得分:-1)

 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();
        }

    }