Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android facebook api post to wall with image

I would like to be able to use the facebook android sdk and post a link to facebook. An example of what I want would be is if you were on facebook and you type a link into your status part, like "http://www.google.com". When you do this a box pops up and your post ends up being a block that has an image and a link. I found documentation in the facebook api for this using an attatchment, though when I try to do this with the android facebook api it doesn't seem to work. I've looked for hours on the net, with no luck. Thanks.

like image 446
Kelly Elton Avatar asked Nov 04 '10 10:11

Kelly Elton


People also ask

How do I post a photo from Facebook Graph API?

Publishing an Photo With that granted, you can upload a photo by issuing an HTTP POST request with the photo content and an optional description to one these to Graph API connections: https://graph.facebook.com/USER_ID/photos - The photo will be published to an album created for your app.

How do I share a Facebook post with API?

Yes, you can share using the graph2 api. The way you do it is to use /feed edge and pass the post's url that you want to share as the link. Standard Fb permissions to the post you are sharing do apply. This was done today, in a local rails app, using FbGraph2 gem, with the above method.


2 Answers

Asuming when you read this that you know how to log onto facebook and such via the api...

  private void fbImageSubmit(Facebook fb, String imageurl, String caption, String description, String name, String linkurl)
    {
        if(fb != null)
        {
            if(fb.isSessionValid())
            {
                Bundle b = new Bundle();
                b.putString("picture", imageurl);
                b.putString("caption",caption);
                b.putString("description",description );
                b.putString("name",name);
                b.putString("link",linkurl);
                try {
                    String strRet = "";
                    strRet = fb.request("/me/feed",b,"POST");
                    JSONObject json;
                    try {
                        json = Util.parseJson(strRet);
                        if(!json.isNull("id"))
                        {
                            Log.i("Facebook", "Image link submitted.");
                        }
                        else
                        {
                            Log.e("Facebook","Error: " + strRet);
                        }
                    } catch (FacebookError e) {
                        Log.e("Facebook","Error: " + e.getMessage());
                    }
                } catch (Exception e) {
                    Log.e("Facebook", "Error: " + e.getMessage());
                }
            }
        }
    }
like image 53
Kelly Elton Avatar answered Oct 06 '22 00:10

Kelly Elton


This works perfect fine with Progress Dialog box.. I have used it...

You must added the jar of Facebook...

  Facebook authenticatedFacebook = new Facebook(APP_ID);

  private static final String[] PERMISSIONS = new String[] { "publish_stream", "read_stream", "offline_access" };

Call below function on button Click....

  authenticatedFacebook.authorize(YOUR_CLASS_NAME.this, PERMISSIONS, new FaceBookWallPostListener());

Now Add this class...

  public class FaceBookWallPostListener implements DialogListener {

    public void onComplete(Bundle values) {
        new FacebookWallPost().execute();
    }

    public void onCancel() {
    }

    public void onError(DialogError e) {
        e.printStackTrace();
    }

    public void onFacebookError(FacebookError e) {
        e.printStackTrace();
    }
}

@Override
protected boolean isRouteDisplayed() {
    return false;
}





 private class FacebookWallPost extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        try {
            path = "Path OF YOUR IMAGE";
            Bundle parameters = new Bundle();
            parameters.putString("message", "MESSAGE YOU WANT TO POST");
            try {
                File file = new File(path, "IMAGE_NAME.jpg");
                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                byte[] data = null;
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
                data = baos.toByteArray();
                if (data != null) {
                    parameters.putByteArray("picture", data);
                }
                parameters.putString("access_token", authenticatedFacebook.getAccessToken());
                authenticatedFacebook.request("me");
                authenticatedFacebook.request("me/photos", parameters, "POST");
            } catch (Exception e) {
                return e.getMessage();
            }

            return "success";
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @Override
    protected void onPostExecute(String result) {
        pDialog.dismiss();
        if (result.equals("success")) {
            Toast.makeText(YOUR_CLASS_NAME.this, "WallPost Successfully Done", Toast.LENGTH_SHORT).show();
            try {
                new File(Environment.getExternalStorageDirectory().toString() + "/Diegodeals", "diegodeals.jpg").delete();
            } catch (Exception e) {
            }

        } else {
            Toast.makeText(YOUR_CLASS_NAME.this, "Failed to post \n " + result, Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onPreExecute() {
        pDialog = new ProgressDialog(YOUR_CLASS_NAME.this);
        pDialog.setMessage("Posting Picture & Message on Facebook...");
        pDialog.show();
    }

}

/////GOOOD LUCK.

like image 45
Siddhpura Amit Avatar answered Oct 05 '22 22:10

Siddhpura Amit