gpt4 book ai didi

android - 无法将图片从 drawable 发布到 facebook

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:29:55 27 4
gpt4 key购买 nike

我正在尝试将图像从 drawables 文件夹传递到 feed 对话框。但我无法在 facebook 提要对话中查看图像。其余参数可用。我正在使用 Facebook SDK 3.5。这是显示提要对话框的函数。

 private void publishFeedDialog() {


Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitMapData = stream.toByteArray();

Bundle params = new Bundle();
params.putByteArray("picture", bitMapData);
params.putString("name", "Facebook SDK for Android");
params.putString("caption", "Build great social apps and get more installs.");
params.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
params.putString("link", "https://developers.facebook.com/android");
//params.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");


WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(getActivity(),
Session.getActiveSession(),
params))
.setOnCompleteListener(new OnCompleteListener() {

@Override
public void onComplete(Bundle values,
FacebookException error) {
if (error == null) {
// When the story is posted, echo the success
// and the post Id.
final String postId = values.getString("post_id");
if (postId != null) {
Toast.makeText(getActivity(),
"Posted story, id: "+postId,
Toast.LENGTH_SHORT).show();
} else {
// User clicked the Cancel button
Toast.makeText(getActivity().getApplicationContext(),
"Publish cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
Toast.makeText(getActivity().getApplicationContext(),
"Publish cancelled",
Toast.LENGTH_SHORT).show();
} else {
// Generic, ex: network error
Toast.makeText(getActivity().getApplicationContext(),
"Error posting story",
Toast.LENGTH_SHORT).show();
}
}

})
.build();
feedDialog.show();
}

最佳答案

发布 Feed 仅适用于图像的 url

查看 feed 文档下的 picture 属性:https://developers.facebook.com/docs/reference/dialogs/feed/

如果你想从你的内存中发布图像(比如可绘制文件夹),那么你需要使用:Request.newUploadPhotoRequest()

除此之外,您还可以使用这个支持 SDK 3.5 的简单开源库以非常简单的方式执行发布照片、提要等操作:https://github.com/sromku/android-simple-facebook

更新

选项 1
您可以使用 Request.newUploadPhotoRequest(),但此方法不允许您添加除图像本身之外的任何其他属性。

Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),R.drawable.ic_launcher);
Request.newUploadPhotoRequest(Session.getActiveSession(), bitmap , new Request.Callback()
{
@Override
public void onCompleted(Response response)
{
// ... handle the response...
}
});

选项 2
如果您想向图像添加其他属性(如描述),则执行几乎相同的操作,但使用原始图形 api 调用。 Request.newUploadPhotoRequest() 的 facebook 实现完全相同,但没有设置额外的属性。

Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),R.drawable.ic_launcher);

Bundle params = new Bundle();
params.putParcelable("picture", bitmap);
params.putString("message", "This is the description of the image");
params.putString("place", "1235456498726"); // place id of the image

Request request = new Request(session, "me/photos", bundle, HttpMethod.POST, new Request.Callback()
{
@Override
public void onCompleted(Response response)
{
// ... handle the response...
}
});

RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();

关于android - 无法将图片从 drawable 发布到 facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19145375/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com