繁体   English   中英

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

[英]Android Facebook v4.0 Post Image and Text

 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:空,错误:{HttpStatus:400,errorCode:324,errorType:OAuthException,errorMessage:(#324)需要上传文件}}

请在Json Object param中我需要通过什么?

谢谢

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

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

公共无效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();
    }

}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM