How to post Bitmap to facebook using facebook sdk?

Deadly 提交于 2019-11-28 11:15:37

问题


I don't find a correct solution to post an image to my facebook wall using facebook sdk. On stackOverflow I found the following solution but AsyncFacebookRunner and mAsyncRunner.request are deprecated and they don't work.

private Facebook facebook;
@SuppressWarnings("deprecation")
public void shareImg(View v) {

    System.out.println("ciao");
    Bitmap img = albero.getDrawingCache();
    if (img != null) {  
        byte[] data = null;

        Bitmap bi = img;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        data = baos.toByteArray();

        Bundle params = new Bundle();
        params.putString("method", "photos.upload");
        params.putByteArray("picture", data);

        AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
        mAsyncRunner.request("me/posts", params, "POST", new SampleUploadListener(), null);

    }
}

Any suggestions?


回答1:


The complete solution :

Request.Callback uploadPhotoRequestCallback = new Request.Callback() {
    @Override
    public void onCompleted(Response response) {
        if (response.getError() != null) { 
            //post error
        } else{
             String idRploadResponse = (String) response.getGraphObject().getProperty("id");
             if (idRploadResponse!= null) { 

                String fbPhotoAddress = "https://www.facebook.com/photo.php?fbid=" +idRploadResponse;                             
             } else { 
                   //error
             } 

        }
    }
};


@SuppressWarnings("deprecation")
public void shareImg(View v) {
    Bitmap img = albero.getDrawingCache(); //I get Btimap from View 

    if (img != null) {

        Request request = Request.newUploadPhotoRequest(Session.getActiveSession(), img,  uploadPhotoRequestCallback);
        Bundle parameters = request.getParameters(); // <-- THIS IS IMPORTANT
        parameters.putString("message", "My message");
        // add more params here
        request.setParameters(parameters);
        request.executeAsync();
    }
}



回答2:


You have to use the function newUploadPhotoRequest see the documentation here :

https://developers.facebook.com/docs/reference/android/3.0/Request#newUploadPhotoRequest%28Session,%20Bitmap,%20Callback%29



来源:https://stackoverflow.com/questions/20908219/how-to-post-bitmap-to-facebook-using-facebook-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!