如何从Android应用程序发布图像到Facebook

时间:2012-06-15 12:49:52

标签: android facebook image

  

可能重复:
  Android post picture to Facebook wall

我是facebook android sdk的新手。我已经将sdk添加到eclipse作为android项目。在我的应用程序中有一个图像和一个按钮。我希望按下按钮时将图像上传到Facebook。我已经阅读过关于SO的各种帖子。但我无法弄清楚如何去做。有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

试试这个,

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

}

答案 1 :(得分:0)

将图像存储在位图中。并使用以下代码。

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(
        "https://graph.facebook.com/me/photos?access_token="
                + AccessTokens.fbaccesstoken);
MultipartEntity entity = new MultipartEntity(
        HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
entity.addPart("source", new ByteArrayBody(data, "myImage.jpg"));
entity.addPart("message", new StringBody(caption.getText()
        .toString()));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,localContext);