Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
704 views
in Technique[技术] by (71.8m points)

android - How to post Bitmap to facebook using facebook sdk?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...