将图像从Android应用程序发布到facebook应用程序墙

时间:2012-09-19 17:57:24

标签: android facebook facebook-wall

我正在尝试将用户使用我的Android应用创建的图片发布到Facebook上的应用程序墙。我已经知道如何在用户的墙上发布图像。

我发现了如何使用php进行本教程:
http://jorgealbaladejo.com/2011/06/13/publish-to-facebook-page-or-applications-wall-with-php/

但我不知道如何获取 page_id 或如何使用适用于Android的Facebook sdk。


修改

我在Facebook墙上发布的代码是:

public void postImageonWall(String path) {

    byte[] data = null;

    Bitmap bi = BitmapFactory.decodeFile(path);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    data = baos.toByteArray();


    Facebook facebook = ((MainActivity) appcontext).getFacebook();

    Log.d("Facebook-PostImage","facebook: " + facebook);
    Bundle params = new Bundle();
    params.putString(Facebook.TOKEN, facebook.getAccessToken());
    params.putString("method", "photos.upload");
    params.putString("message", "Message Text");
    params.putString("link","ANDROID_MARKET_LINK"); //or any other link
    params.putString("name", "APP/GAME NAME");
    params.putByteArray("picture", data);


    Log.d("Facebook-PostImage","AsyncRunner going to be called");
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
    mAsyncRunner.request(null, params, "POST", new RequestListener() {
        public void onComplete(final String response, final Object state) {
                try {
                    // process the response here: (executed in background thread)
                    Log.d("Facebook-Example", "Response: " + response.toString());
                    JSONObject json = Util.parseJson(response);
                    final String src = json.getString("src");

                    // then post the processed result back to the UI thread
                    // if we do not do this, an runtime exception will be generated
                    // e.g. "CalledFromWrongThreadException: Only the original
                    // thread that created a view hierarchy can touch its views."

                } catch (JSONException e) {
                    Log.w("Facebook-Example", "JSON Error in response");
                } catch (FacebookError e) {
                    Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
                }
            }
        @Override
        public void onFacebookError(FacebookError e, Object state) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onIOException(IOException e, Object state) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onFileNotFoundException(FileNotFoundException e,
                Object state) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onMalformedURLException(MalformedURLException e,
                    Object state) {
                // TODO Auto-generated method stub
            }
          }, null);
}

1 个答案:

答案 0 :(得分:0)

这是我用于获取应用程序页面ID的代码:

private JSONObject getJSONfromUrl(String url) throws IOException {
    InputStream is = new URL(url).openStream();
    try {
      BufferedReader rd = new BufferedReader(new InputStreamReader(is,      Charset.forName("UTF-8")));
      String jsonText = readAll(rd);
      JSONObject json = new JSONObject(jsonText);
      return json;
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
      is.close();
    }
    return null;
}

private String getPageId(String url) {
    String identifier = url.substring(url.lastIndexOf("/"));
    String graphUrl = "https://graph.facebook.com/" + identifier;

    JSONObject response;
    String id = null;
    try {
       response = getJSONfromUrl(graphUrl);
       id = (String) response.get("id");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return id;
}
相关问题