如何从SD卡上传图像到Facebook

时间:2013-05-08 06:31:04

标签: android facebook

我已经使用Facebook SDK完成了文本共享。我被困在从SD卡上传图片。在将图像解析为FB SDK之前,首先我将图像转换为位图然后我应该与Facebook共享。但这种逻辑不起作用。它永远不会从SD卡上传图像。任何人都可以指导我如何从SD卡上传图像?提前谢谢。

Bitmap bi = BitmapFactory.decodeStream(fis);
Bitmap bi =BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() +"/Android/test.jpg", options);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);

2 个答案:

答案 0 :(得分:1)

尝试下面的代码,并检查你的位图不应为null ..

Bundle bundle = new Bundle();
bundle.putString("message", "post image.." );

byte[] data = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
<<your bitmap>>.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();

bundle.putString(Facebook.TOKEN, facebook.getAccessToken());
bundle.putString("method", "photos.upload");
bundle.putByteArray("picture", data);
bundle.putString("message", "post image.." );

mAsyncRunner.request(null,bundle,"POST",new RequestListener() {
    @Override
    public void onMalformedURLException(MalformedURLException e, Object state) {
      Log.d("request RequestListener", "debug onMalformedURLException");
    }
    @Override
    public void onIOException(IOException e, Object state) {
  Log.d("request RequestListener", "debug onIOException");
    }
    @Override
    public void onFileNotFoundException(FileNotFoundException e, Object state) {
  Log.d("request RequestListener", "debug onFileNotFoundException");
    }
    @Override
    public void onFacebookError(FacebookError e, Object state) {
  Log.d("request RequestListener", "debug onFacebookError");
    }
    @Override
    public void onComplete(String response, Object state) {
   Log.d("request RequestListener", "debug onComplete");
    }
}, null);

答案 1 :(得分:0)

试试这个。

 private Facebook facebook = new Facebook("319*********897");
 byte[] byteArray;

从SD卡获取图像并转换为byteArray。

然后使用此功能在Facebook上分享图像。

 public void shareOnFacebook() {
    // mDuckHuntView.pause();

    facebook.authorize(this, new String[] { "publish_stream",
            "user_photos", "publish_checkins", "publish_actions" },
            new DialogListener() {
                public void onComplete(Bundle values) {
                    try {

                        Bundle bundle = new Bundle();
                        bundle.putString(
                                "name",
                                "text here");
                        bundle.putByteArray("file", byteArray);
                        bundle.putByteArray(Facebook.TOKEN, values
                                .getString(Facebook.TOKEN).getBytes());
                        String response = facebook.request("me/photos",
                                bundle, "POST");

                        Log.d("UPDATE RESPONSE", "" + response);
                        JSONObject json = Util.parseJson(response);
                        if (!json.isNull("id")) {
                            // err("Facebook Image link submitted.");

                        } else {
                            // err("Facebook Error: " + response);
                        }

                    } catch (Exception e) {


                    } catch (FacebookError e) {

                    }
                }

                public void onFacebookError(FacebookError e) {

                }

                public void onError(DialogError e) {

                }

                public void onCancel() {

                }

            });
}

为此你需要使用facebook sdk包。