Android Facebook v4.0发布图片和文字

时间:2015-04-02 13:20:05

标签: android facebook image post

 JSONObject jsonObject = new JSONObject();
    try {

        jsonObject.put("picture", bitmap);
        jsonObject.put("message","just a message");

    } catch (JSONException e) {
        e.printStackTrace();
    }


    GraphRequest graphRequest = GraphRequest.newPostRequest(AccessToken.getCurrentAccessToken(), "me/photos", jsonObject, new GraphRequest.Callback() {
        @Override
        public void onCompleted(GraphResponse graphResponse) {

            Log.d("", "------ graphResponse = " + graphResponse);

        }
    });


    graphRequest.executeAsync();

正在回归

  

graphResponse = {响应:responseCode:400,graphObject:null,错误:{HttpStatus:400,errorCode:324,errorType:OAuthException,errorMessage:(#324)需要上传文件}}

请在Json Object param中我需要传递什么?

感谢

3 个答案:

答案 0 :(得分:5)

而不是传递json对象,只需传递一个包。

        GraphRequest request = GraphRequest.newPostRequest(token,
                "me/photos", null, callback2);

        Bundle postParams = request.getParameters();

        postParams.putByteArray("picture", MyUtils.FileToBytes("path to local file"));

        postParams.putString("caption", "my picture");

        request.setParameters(postParams);

        request.executeAsync();

答案 1 :(得分: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);
        byte[] data = null;

        Bitmap bi = BitmapFactory.decodeResource(getResources(),
                R.drawable.ll);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        data = baos.toByteArray();

        //  parameters.putByteArray("image", bitmapdata);



        GraphRequest graphRequest = GraphRequest.newPostRequest(AccessToken.getCurrentAccessToken(), "me/photos", null, new GraphRequest.Callback() {


            @Override
                    public void onCompleted(GraphResponse graphResponse) {



                    }
                    });
        Bundle postParams = graphRequest.getParameters();

        postParams.putByteArray("picture", data);

        postParams.putString("caption", message.getText().toString());

        graphRequest.setParameters(postParams);

        graphRequest.executeAsync();




    }
    else
    {

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

}

答案 2 :(得分:0)

newPostRequest用于创建GraphObjects,而不用于照片。使用SDK的v4.0,您应该创建一个ShareContent,然后使用ShareApi共享它,例如:

Bitmap image = ...
SharePhoto photo = new SharePhoto.Builder()
    .setBitmap(image);
    .build();
SharePhotoContent content = new SharePhotoContent.Builder()
    .addPhoto(photo)
    .build();
ShareApi.share(content, new FacebookCallback<Sharer.Result>() { ...});

有关详细信息,请参阅https://developers.facebook.com/docs/sharing/android