0

I'm trying to publish images created by the user with my Android app to the application wall on Facebook. I already know how to publish the images on the user's wall.

I found this tutorial of how to do it with php:
http://jorgealbaladejo.com/2011/06/13/publish-to-facebook-page-or-applications-wall-with-php/

But I don't know how to obtain the page_id or how to do it with the Facebook sdk for Android.


EDIT

My code for post on facebook wall is:

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);
}
Kara
  • 6,115
  • 16
  • 50
  • 57
goda87
  • 31
  • 5
  • You aren't sure how to get the page_id of your page ? Check here for several examples - http://stackoverflow.com/questions/10149209/facebook-graph-api-get-id-from-facebook-page-url. Once you have the page_id, you can post photos to that page via - https://developers.facebook.com/docs/reference/api/page/#photos – deesarus Sep 19 '12 at 18:17
  • Thank you, I will try with your advices. – goda87 Sep 20 '12 at 08:27
  • Thank you @deesarus. Now, I can get the **page_id**, but I don't know how to use it with my code. – goda87 Sep 20 '12 at 09:11
  • I changed my `mAsyncRunner.request(null, params, "POST", new RequestListener() { ... ` by `mAsyncRunner.request(pageid + "/feed", params, "POST", new RequestListener() { ... ` but I get a Facebook Error: Unsupported method, photos.upload. – goda87 Sep 21 '12 at 07:59

1 Answers1

0

This is the code I used for get my Application Page 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;
}
goda87
  • 31
  • 5