不能在Facebook上发布图片

时间:2012-09-07 16:33:27

标签: android facebook facebook-graph-api

我能够在facebook上成功发布文字,但是当我在Facebook上发布带有图片的消息时,我无法在脸谱上发布图片,我也没有收到任何错误但只是得到了

       json.isNull("id") =  null 

我使用过像

这样的许可
       private static final String[] PERMISSIONS = new String[] { "publish_stream", "read_stream", "offline_access" };

我的代码是

      try {
            // String response = authenticatedFacebook.request("me");
            // JSONObject obj = Util.parseJson(response);
            path = Environment.getExternalStorageDirectory().toString() + "/Diegodeals";
            Bundle parameters = new Bundle();
            parameters.putString("message", txtTitle.getText().toString() + "\n" + txtDesc.getText().toString());
            File file = new File(path, "diegodeals.jpg");
            System.out.println(file.getAbsolutePath());
            Bitmap bitmap = getResizedBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()), 120, 120);
            byte[] data = null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            data = baos.toByteArray();
            if (data != null) {
                parameters.putByteArray("picture", data);
            }
            parameters.putString("access_token", authenticatedFacebook.getAccessToken());
            authenticatedFacebook.request("me");
            String response = authenticatedFacebook.request("me/feed", parameters, "POST");
            JSONObject json;
            try {
                json = Util.parseJson(response);
                if (!json.isNull("id")) {
                    isWallPostSuccess = false;
                    return "failed";
                } else {
                    isWallPostSuccess = true;
                    return "success";
                }
            } catch (FacebookError e) {
                isWallPostSuccess = false;
                e.printStackTrace();
            }

        } catch (Throwable e) {
            isWallPostSuccess = false;
            e.printStackTrace();
        }
        return null;
    }

    public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // CREATE A MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scaleWidth, scaleHeight);

        // RECREATE THE NEW BITMAP
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
        return resizedBitmap;
    }

2 个答案:

答案 0 :(得分:0)

picture参数接收图片的URL,而不是图片二进制本身。将图片放在一些可公开访问的服务器上并将其链接到那里,或使用Facebook的图形API /photos图片上传机制。

答案 1 :(得分:0)

将Facebook上的照片上传到自己的墙上的正确方法是将参数“photo”和“caption”捆绑到“me / photos”。请注意,您的“图片”和“消息”指向“我/ Feed”,但这不起作用。

在您的示例中,您似乎拥有要上传的本地图片。这是怎么做的:

Bundle params = new Bundle();
params.putByteArray(
        "photo",
        Util.getByteArrayFromDrawable(getResources().getDrawable(
                R.drawable.picture)));
params.putString("caption", "test message here");
mAsyncRunner.request("me/photos", params, "POST", new PhotoUploadListener());

否则,为“picture”参数提供一个URL(而不是字节数组),为“caption”参数提供一个字符串,并发布到“me / feed”。