gpt4 book ai didi

android - Facebook SDK : share local picture with text is driving me crazy

转载 作者:行者123 更新时间:2023-11-30 02:45:36 26 4
gpt4 key购买 nike

目标

在我的相机应用中,我想让用户在用户的 Facebook 墙上分享一张带有预格式化文本和描述的图片。


简介

我在谷歌上搜索了很多并关注了“the terrific” facebook-getting-started ,尝试了很多东西,持续了 !!days!!... 还没有找到完全可行的解决方案。

至少我认为我得到了一些分数:

  1. 出色的 Android Intent action_send 效果很好但不选择 facebook,它适用于文本或图片,但不适用于两者 (!!),请参阅 here

  2. applicationID、hashxxx 以及将 android 应用程序链接到 facebook 登录所需的所有其他内容,所有我真的不理解和不想理解的东西,完成并最终工作(感谢 facebook 的所有这些 sh *t!!)

  3. 可以与 facebook 分享(参见 here ):
    3a.使用 facebook 应用程序(安装在设备中)
    3b.使用登录 session (如果未安装 Facebook 应用程序)

  4. Facebook 要我们使用它的 SDK,这很 [b|s] 广告,但我可以忍受。

  5. 案例 3a - facebook 建议我们使用 shareDialog,并且 - 与建议的代码 fragment 和示例进行了很多斗争! - 我已经能够做到,但如果 facebook 应用程序不是,我也需要它来工作安装(案例 3b。)

  6. 案例 3b - facebook 建议使用“丑陋的”feedDialog 作为后备(参见 here)。
    6a. feedDialog 需要登录,没关系...
    6b. feedDialog好像不能分享本 map 片,我真的不明白为什么... here facebook 指南仅谈论“图片”关键字的“URL”...
    5c.然后我想我必须使用 Request,我尝试实现它但没有任何反应,我想我遗漏了一些东西...没有找到有用的引用(=工作示例)。


代码 fragment 从 SO 和 developers.facebook 复制/粘贴/编辑

ShareDialog 和 session 管理,以防未安装 Facebook 应用正常运行

/**
* share image with facebook, using facebook sdk
* @param pathToImage
*/
private void shareWithFacebook(String pathToImage) {
Log.i("SHARE", "share with facebook");
if (FacebookDialog.canPresentShareDialog(getApplicationContext(), FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) {
// Publish the post using the Share Dialog
Log.i("SHARE", "share with ShareDialog");
FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this)
.setCaption("Sharing photo taken with MyApp.")
.setName("Snail Camera Photo")
.setPicture("file://"+pathToImage)
.setLink("http://myapplink")
.setDescription("Image taken with MyApp for Android")
.build();
uiHelper.trackPendingDialogCall(shareDialog.present());
} else {
// maybe no facebook app installed, trying an alternative
// here i think i need a session
if (Session.getActiveSession() != null && Session.getActiveSession().isOpened()) {
publishFeedDialog(pathToImage);
} else {
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {

List<String> permissions = new ArrayList<String>();
permissions.add("publish_actions");

session.openForRead(new Session.OpenRequest(this)
.setPermissions(permissions)
.setCallback(mFacebookCallback));
} else {
Session.openActiveSession(this, true, mFacebookCallback);
}
}
}
}

private Session.StatusCallback mFacebookCallback = new Session.StatusCallback() {
public void call(final Session session, final SessionState state, final Exception exception) {
if (state.isOpened()) {
String facebookToken = session.getAccessToken();
Log.i("SHARE", facebookToken);
Request.newMeRequest(session, new Request.GraphUserCallback() {
public void onCompleted(GraphUser user, com.facebook.Response response) {
publishFeedDialog(MP.LAST_TAKEN_FOR_GALLERY);
}
}).executeAsync();
}
}
};



feedDialog fragment ,适用于文本字段、链接和远程 url,但不适用于使用“file://...”或“file:///...”的本 map 片 .错误消息:图片 URL 的格式不正确

private void publishFeedDialog(String pathToImage) {
Bundle params = new Bundle();
params.putString("name", "myapp Photo");
params.putString("caption", "Sharing photo taken with myapp.");
params.putString("description", "Image taken with myapp for Android");
params.putString("link", "http://myapp.at.playstore");
params.putString("picture", "file://"+pathToImage);


WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(EnhancedCameraPreviewActivity.this,//getApplicationContext(),
Session.getActiveSession(),
params))
.setOnCompleteListener(new OnCompleteListener() {

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

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



另一个尝试,使用 Request,它说:

photo upload problem. Error={HttpStatus: 403, errorCode: 200, errorType: OAuthException, errorMessage: (#200) Requires extended permission: publish_actions}

我尝试在上面的 session 管理中添加 publish_actions 权限,也许我错过了什么......

private void publishFeedDialog(String pathToImage) {
Request request=Request.newPostOpenGraphObjectRequest(
Session.getActiveSession(),
"PhotoUpload",
"myApp Photo Upload",
"file://"+pathToImage,
"http://myapp.at.playstore",
"Image taken with myApp for Android",
null,
uploadPhotoRequestCallback);
request.executeAsync();
}



最后尝试使用 Request,无论是否使用“picture”关键字,都没有任何反应。

private void publishFeedDialog(String pathToImage) {
Bundle parameters = new Bundle();
parameters.putString("message", "Image taken with myApp for Android");
parameters.putString("picture", "file://"+pathToImage);
parameters.putString("caption", "myApp Photo");

Request request = new Request(Session.getActiveSession(), "/me/feed", parameters, null);
// also tried: Request request = new Request(Session.getActiveSession(), "/me/feed", parameters, com.facebook.HttpMethod.POST);
request.executeAsync();
}



问题

-1- 我的 1..6 点是否有错误?

-2- 我可以使用 FeedDialog 分享本 map 片吗?

-3-如果没有,没有安装facebook app怎么办?

非常感谢!

最佳答案

我有同样的问题,但我有解决方法。

  1. 我在您的第 1-6 点中找不到任何错误的推理,看起来像我遇到的相同问题。
  2. 您不能在本地分享图片,但是...
  3. 下面的代码 fragment :

这会将图片上传到用户个人资料并将其张贴在他\她的墙上,之后您可以根据需要获取 URL:

private void uploadPicture(final String message, Session session) {
Request.Callback uploadPhotoRequestCallback = new Request.Callback() {
@Override
public void onCompleted(com.facebook.Response response) {
if (response.getError() != null) {
Toast.makeText(getActivity(), "Failed posting on the Wall", Toast.LENGTH_LONG).show();
return;
}

Object graphResponse = response.getGraphObject().getProperty("id");
if (graphResponse == null || !(graphResponse instanceof String) ||
TextUtils.isEmpty((String) graphResponse)) {
Toast.makeText(getActivity(), "Failed uploading the photo\no respons", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Succsefully posted on the facebook Wall", Toast.LENGTH_LONG).show();
}
}
};

// Execute the request with the image and appropriate message
Request request = Request.newUploadPhotoRequest(session, profileBitmap, uploadPhotoRequestCallback);
Bundle params = request.getParameters();
params.putString("message", message);
request.executeAsync();
}

在没有APP的情况下启动facebook登录,更简单,我使用以下代码 fragment 在facebook按钮上调用facebook SDK:

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.faceBookButton:
if (((imagePath != null && !imagePath.equals("")) || (message != null && !message.equals("")))) {
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
session.openForPublish(new Session.OpenRequest(this)
.setPermissions(Arrays.asList("public_profile", "publish_actions"))
.setCallback(statusCallback));
} else {
if (profileBitmap != null) {
uploadPicture(message, session);
} else {
publishFeedDialog(message);
}
}
} else {
Toast.makeText(getActivity(), getString(R.string.checkin_fail), Toast.LENGTH_SHORT).show();
}
break;
}
}

在我的例子中,publishFeedDialog 接受您要传递的消息,而不是图像,但这并不重要。无论如何,它都会打开 Facebook 的消息对话框,我还没有找到从我的 EditText 小部件传递预定义消息的方法。

关于android - Facebook SDK : share local picture with text is driving me crazy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25095996/

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