簡體   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