1

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 a button. I want the image to be uploaded to facebook when button is pressed. I have read various posts on SO. But I am not able to figure out how to go about it. Any help ?

Community
  • 1
  • 1
John Watson
  • 869
  • 3
  • 16
  • 32

1 Answers1

0

Try with this,

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

mButton.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             Facebook mFacebook=new Facebook(yourAppID)

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

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

    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
    mAsyncRunner.request(null, params, "POST",
            new SampleUploadListener(), null);
         }
     });

public class SampleUploadListener extends BaseRequestListener {

    @SuppressWarnings("unused")
    public void onComplete(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(new Runnable() {
                public void run() {
                    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());
        }
    }
}

public abstract class BaseRequestListener implements RequestListener {

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

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

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

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

}
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
Aerrow
  • 12,086
  • 10
  • 56
  • 90