android facebook api发布到墙上的图像

时间:2010-11-04 10:50:33

标签: android facebook api

我希望能够使用facebook android sdk并发布到facebook的链接。我想要的一个例子是,如果您在Facebook上并且键入了状态部分的链接,例如“http://www.google.com”。当您执行此操作时,会弹出一个框,您的帖子最终会成为具有图像和链接的块。我在facebook api中找到了使用附件的文档,但是当我尝试使用android facebook api时,它似乎不起作用。我在网上看了几个小时,没有运气。感谢。

2 个答案:

答案 0 :(得分:16)

当你读到这篇文章时,你知道如何通过api ...登录facebook等等。

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

答案 1 :(得分:3)

这与进度对话框完美搭配..我已经用过它了......

你必须添加Facebook的jar ...

  Facebook authenticatedFacebook = new Facebook(APP_ID);

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

在按钮上调用以下功能点击....

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

现在添加此类......

  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。