我想使用Graph API在Facebook上发布图像。我已经将图像转换为字节数组并尝试发布它,但是我失败了。它显示贴在墙上的文字,但图像不张贴。我不知道什么是错的。使用GraphApi在Facebook上发布图像不起作用
我已经检查了以下可用的解决方案,但他们都没有为我工作。
1)How to post image from drawable folder to facebook with graph api?
2)Unable to post an image from drawable to facebook
3)Post image with text on facebook from android
4)Android photo upload to facebook using graph api?
这里是我试图的代码。
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();
final Bundle params = new Bundle();
params.putString("name", "Test FB Post.");
params.putString("method", "photos.upload");
params.putByteArray("picture", bitMapData);
//Tried below code but not working.
/*try {
String response = mFacebook.request("me/photos", params, "POST");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(m_context,
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(m_context,
"Posted story, id: " + postId,
Toast.LENGTH_SHORT).show();
finish();
} else {
// User clicked the Cancel button
Toast.makeText(m_context, "Publish cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
Toast.makeText(m_context, "Publish cancelled",
Toast.LENGTH_SHORT).show();
} else {
// Generic, ex: network error
Toast.makeText(getApplicationContext(),
"Error posting story", Toast.LENGTH_SHORT)
.show();
}
}
}).build();
feedDialog.show();
}
请指导我。任何帮助将不胜感激。
感谢
我已经解决了我的问题: 照片可以被创建。请检查我的答案。 – GrIsHu