Shreyash Mahajan
Shreyash Mahajan

Reputation: 23606

Android: How to post the message with the image post on facebook?

In My Appplication i am using this code to post the photo on the Facebook.

Code:

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

Now I am able to post the Photo with this code. But now i want to post the message with this photo post. So what else i have to do ?

Please help me regarding this. Thanks.

Upvotes: 0

Views: 7843

Answers (2)

Sock
Sock

Reputation: 428

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;

        }
    }

Upvotes: 3

Andro Selva
Andro Selva

Reputation: 54330

Simply you have to add a extra parameter to the Bunlde object params. Here is wat I do,

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

I think you are missing this line.

Upvotes: 5

Related Questions