Skip to content Skip to sidebar Skip to footer

How To Post Image To Facebook From Android App

Possible Duplicate: Android post picture to Facebook wall I am new to facebook android sdk. I have added the sdk to eclipse as android project. In my app there is an image and

Solution 1:

Store you image in a Bitmap. And use the following code.

HttpClienthttpClient=newDefaultHttpClient();
HttpContextlocalContext=newBasicHttpContext();
HttpPosthttpPost=newHttpPost(
        "https://graph.facebook.com/me/photos?access_token="
                + AccessTokens.fbaccesstoken);
MultipartEntityentity=newMultipartEntity(
        HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStreambos=newByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
entity.addPart("source", newByteArrayBody(data, "myImage.jpg"));
entity.addPart("message", newStringBody(caption.getText()
        .toString()));
httpPost.setEntity(entity);
HttpResponseresponse= httpClient.execute(httpPost,localContext);

Solution 2:

Try with this,

Button mButton=(Button)findViewById(R.id.button);

mButton.setOnClickListener(newView.OnClickListener() {
         publicvoidonClick(View v) {
             Facebook mFacebook=newFacebook(yourAppID)

    byte[] data = null;
    Bitmap bi = BitmapFactory.decodeFile(imageLink);
    ByteArrayOutputStream baos = newByteArrayOutputStream();
    bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
    data = baos.toByteArray();

    Bundle params = newBundle();
    params.putString("method", "photos.upload");
    params.putByteArray("picture", data);

    AsyncFacebookRunner mAsyncRunner = newAsyncFacebookRunner(mFacebook);
    mAsyncRunner.request(null, params, "POST",
            newSampleUploadListener(), null);
         }
     });

publicclassSampleUploadListenerextendsBaseRequestListener {

    @SuppressWarnings("unused")
    publicvoidonComplete(final String response, final Object state) {
        try {
            Log.d("Facebook-Example", "Response: " + response.toString());
            JSONObject json = Util.parseJson(response);
            String src = json.getString("src");

            PublishImage.this.runOnUiThread(newRunnable() {
                publicvoidrun() {
                    Toast.makeText(getApplicationContext(),
                            "Successfully Uploaded", Toast.LENGTH_SHORT)
                            .show();
                }
            });
        } catch (JSONException e) {
            Log.w("Facebook-Example", "JSON Error in response");
        } catch (FacebookError e) {
            Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
        }
    }
}

publicabstractclassBaseRequestListenerimplementsRequestListener {

    publicvoidonFacebookError(FacebookError e, final Object state) {
        Log.e("Facebook", e.getMessage());
        e.printStackTrace();
    }

    publicvoidonFileNotFoundException(FileNotFoundException e,
            final Object state) {
        Log.e("Facebook", e.getMessage());
        e.printStackTrace();
    }

    publicvoidonIOException(IOException e, final Object state) {
        Log.e("Facebook", e.getMessage());
        e.printStackTrace();
    }

    publicvoidonMalformedURLException(MalformedURLException e,
            final Object state) {
        Log.e("Facebook", e.getMessage());
        e.printStackTrace();
    }

}

Post a Comment for "How To Post Image To Facebook From Android App"