从Android上传照片到Facebook墙/相册

时间:2012-04-06 13:55:17

标签: android facebook facebook-android-sdk

我从Git下载了Facebook Android SDK,并按照developers.facebook中的步骤操作,获得了应用程序ID并运行了一些示例代码。我可以在墙上发布状态。现在我试图在墙上上传一张带有描述或标题的照片,但我不能。我浏览过,大多数时候我发现以下代码作为照片上传的答案。

public void postImageonWall() {
    byte[] data = null;

    Bitmap bi = BitmapFactory.decodeFile("/sdcard/cutedog.jpg");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    data = baos.toByteArray();
    Bundle params = new Bundle();
    params.putString(Facebook.TOKEN, mFacebook.getAccessToken());
    params.putString("method", "photos.upload");
    params.putByteArray("picture", data);
    params.putString("caption", "So Cuuteee!!");
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
    mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);
}

为此,我得到了回复     { “PID”: “100003180585600_320223”, “援助”: “100003180585600_43760”, “主人”:100003180585600, “SRC”:“http://photos-g.ak.fbcdn.net/hphotos-ak-prn1/533575_198930480222970_100003180585600_320223_995139351_s。 JPG “ ”src_big“: ”http://a7.sphotos.ak.fbcdn.net/hphotos-ak-prn1/s720x720/533575_198930480222970_100003180585600_320223_995139351_n.jpg“, ”src_small“:” HTTP://photos-g.ak。 fbcdn.net/hphotos-ak-prn1/533575_198930480222970_100003180585600_320223_995139351_t.jpg","link":"http://www.facebook.com/photo.php?fbid=198930480222970&set=a.198930373556314.43760.100003180585600&type=1" ,“标题”:“所以Cuuteee !!”,“创建”:13337186​​24,“object_id”:198930480222970}

将照片上传到某个地方(它会提供一些网址)作为小尺寸照片。但不是Facebook墙上或专辑。通过查看Stack Overflow问题 Android - How to upload photo from the SD card to the Facebook wall ,我尝试了

    mAsyncRunner.request("me/photos", params, "POST", new SampleUploadListener(), null);

    mAsyncRunner.request("me/feed", params, "POST", new SampleUploadListener(), null);

对于两者,我得到以下作为回应

   {"error":{"message":"Unsupported method, photos.upload","type":"Exception"}}

如何在墙上上传?

2 个答案:

答案 0 :(得分:3)

您的应用是否拥有将照片上传到Facebook的权限photo_upload

试试这段代码:

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

FileInputStream in;
BufferedInputStream buf;
try {
    File file = new File("/sdcard/cutedog.jpg");

    in = new FileInputStream(file);
    buf = new BufferedInputStream(in,1070);

    byte[] bMapArray= new byte[buf.available()];
    buf.read(bMapArray);
    params.putByteArray("picture", bMapArray);
} catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

mAsyncRunner.request(null, params, "POST",new SampleUploadListener(), null);

答案 1 :(得分:0)

请参阅https://github.com/facebook/facebook-android-sdk/tree/master/examples/Hackbook/src/com/facebook/android的源代码。

它包含有关如何上传图片的源代码。