Android:如何使用Facebook上的图片发布消息?

时间:2011-12-21 04:21:50

标签: android facebook-graph-api android-layout facebook

在我的应用程序中,我使用此代码在Facebook上发布照片。

代码:

 // For Facebook ===================================
            Button facebookButton = (Button) saveButtonDialog.findViewById(R.id.facebook);
            facebookButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    saveButtonDialog.dismiss();

                    saveImageFunction(); // to save the Image

                    facebook.authorize(TWSBIDrawMainActivity.this, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"},new DialogListener() {                     
                        @Override                     
                        public void onComplete(Bundle values) {   
                            postImageonWall(); 
                            Toast.makeText(getApplicationContext(), "Image Posted on Facebook.", Toast.LENGTH_SHORT).show();

                        }                      
                        @Override                     
                        public void onFacebookError(FacebookError error) {                     
                        }                      
                        @Override                     
                        public void onError(DialogError e) {                     
                        }                      
                        @Override                     
                        public void onCancel() {                     
                        }                 
                    }); 
                }
            });



 public void postImageonWall() {             
    byte[] data = null;               

    Bitmap bi = BitmapFactory.decodeFile(APP_FILE_PATH + "/"+filename+".jpg");
    //Bitmap bi = BitmapFactory.decodeResource(getResources(), R.drawable.icon);             
    ByteArrayOutputStream baos = new ByteArrayOutputStream();              
    bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);              
    data = baos.toByteArray();                
    Bundle params = new Bundle();              
    params.putString(Facebook.TOKEN, facebook.getAccessToken());              
    params.putString("method", "photos.upload");              
    params.putByteArray("picture", data);               
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);              
    mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);    

现在我可以使用此代码发布照片。 但现在我想用这张照片发布消息。那我还有什么需要做的呢?

请帮我解决这个问题。 感谢。

2 个答案:

答案 0 :(得分:5)

只需要为Bunlde对象参数添加一个额外的参数。这是我做的事,

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

params.putString(“caption”,facebook_comment);

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

我想你错过了这一行。

答案 1 :(得分:3)

private String postwall(String uid)
    {
        String response = "";
        try
        {

            String DIRECTORY_PATH = "/sdcard/159.jpg";
            Bundle params = new Bundle();
            Bitmap bitmap = BitmapFactory.decodeFile(DIRECTORY_PATH);
            byte[] data = null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            data = baos.toByteArray();
            params.putString("app_id", uid);
            params.putString("message", "picture caption");
            params.putByteArray("picture", data);

            mFacebook.authorize(this, PERMISSIONS, new LoginDialogListener());
            mAsyncRunner.request("me/photos", params, "POST", new WallPostRequestListener());
            mAsyncRunner.request(response, new WallPostRequestListener());
            Log.e("post result", response);

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return response;
    }

    public class WallPostRequestListener extends BaseRequestListener
    {

        public void onComplete(final String response)
        {
            Log.d("Facebook-Example", "Got response: " + response);
            String message = "<empty>";
            try
            {
                JSONObject json = Util.parseJson(response);
                message = json.getString("message");
            }
            catch (JSONException e)
            {
                Log.w("Facebook-Example", "JSON Error in response");
            }
            catch (FacebookError e)
            {
                Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
            }
            final String text = "Your Wall Post: " + message;

        }
    }