0

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.

Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • you want to send message with image.? and on own wall ya friend's wall.? –  Dec 21 '11 at 04:26
  • please check this issue, i hope you can resolve this issue: http://stackoverflow.com/questions/18657388/how-to-allow-user-to-post-an-image-on-wall-using-sdcard-folder – Sun Sep 09 '13 at 04:51

2 Answers2

5

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.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • Ok Thanks for the reply. Let me check it. – Shreyash Mahajan Dec 21 '11 at 04:45
  • Hi all can some one tell me why am getting error in this line "facebook.authorize(DrawingActivity.this, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"},new DialogListener() {" even after importing the android facebook sdk??? – Quick learner Feb 03 '12 at 11:15
  • Am using the above mentioned code for my application.when am running my app its crashing on clicking the button to post message on facebook wall.. – Quick learner Feb 03 '12 at 13:30
  • I am unable to post picture on wall.... I am getting { "data": [ ] } in response... please help – hharry_tech Apr 15 '14 at 06:51
  • This is more than two years old. I guess this is deprecated already. I would suggest you to follow a latest tutorial from somewhere else. I am sorry I couldn't help. – Andro Selva Apr 15 '14 at 06:52
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;

        }
    }
Sock
  • 418
  • 1
  • 5
  • 12