【问题标题】:facebook API add image along with wall feedfacebook API 添加图像以及墙提要
【发布时间】:2012-11-14 14:02:51
【问题描述】:

是否可以在 Facebook 墙上发布带有图片的消息(不是图片链接,而是图片数据)?

我在http://developers.facebook.com/docs/reference/api/post/https://developers.facebook.com/docs/guides/attachments/ 中都没有发现这种可能性。

我已经准备好忍受不可能做到这一点,但我遇到了 iOS 6.0 中引入的 SLComposeViewController 类的文档(http://developer.apple.com/library/ios/#documentation/NetworkingInternet/参考/SLComposeViewController_Class/Reference/Reference.html)。

这个类有一个方法 - (BOOL)addImage:(UIImage *)image 可以满足我的需要。

我为 Android 编程,因此我无法使用它。但显然这种方法必须使用 facebook API。但我找不到它:与图片发布相关的所有内容都需要 url,而不是数据。

那么,在 Android 中是否可以在 Facebook 墙上发布带有图像数据的消息?

【问题讨论】:

  • 图像数据是指来自画廊或相机等的图像,对吗?如果是,请在几分钟后查看我发布的答案。
  • 不,我是指以编程方式创建的图像。例如,通过调整资产文件夹中现有图像的大小。

标签: android facebook facebook-graph-api post


【解决方案1】:

编辑:我几乎用您的评论发布了答案。您可以使用类似的方法将您的图像从assets 转换为Bitmap

InputStream bitmap = null;

try {
    bitmap = getAssets().open("icon.png");
    bmpImageGallery = BitmapFactory.decodeStream(bitmap);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    bitmap.close();
}

这就是我通过 onActivityResult 方法中的 Intent 显示图库中的图像的方式:

targetURI = data.getData();

try {

    bmpImageGallery = MediaStore.Images.Media.getBitmap(this.getContentResolver(), targetURI);

    // SET THE IMAGE FROM THE GALLERY TO THE IMAGEVIEW
    imgvwSelectedImage.setImageBitmap(bmpImageGallery);

} catch (FileNotFoundException e) {
    e.printStackTrace();

} catch (IOException e) {
    e.printStackTrace();
}

这是上传图片的代码:

byte[] data = null;

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmpImageGallery.compress(CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();

Bundle postImgGallery = new Bundle();

// ADD THE PHOTO DATA TO THE BUNDLE
postImgGallery.putByteArray("photo", data);

// ADD THE CAPTION FROM THE STRING finalStatusMessage TO THE BUNDLE
if (finalStatusMessage.equals(""))  {
    /*****  DO NOTHING HERE     *****/
} else {
    postImgGallery.putString("caption", finalStatusMessage);
}

Utility.mAsyncRunner.request(userID + "/photos", postImgGallery, "POST", new PhotoUploadListener(), null);

注意:在此处"caption", finalStatusMessage 中,caption 也可以替换为message。我从来没有看到使用其中任何一个的帖子有任何区别。但为了安全起见,请在使用前进行检查。 ;-)

这个类是用来检查上传状态的:

private class PhotoUploadListener extends BaseRequestListener   {

    @Override
    public void onComplete(String response, Object state) {
        // DISPLAY A CONFIRMATION TOAST
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    相关资源
    最近更新 更多