简体   繁体   中英

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

And is returning

graphResponse = {Response: responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 324, errorType: OAuthException, errorMessage: (#324) Requires upload file}}

Please in Json Object param what i need to pass?

thanks

instead of passing the json object, just pass the bundle one.

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

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

}

newPostRequest is for creating GraphObjects, and not for photos. Using v4.0 of the SDK, you should create a ShareContent, and then share it using the ShareApi, something like:

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

See https://developers.facebook.com/docs/sharing/android for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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