如何使用 Facebook android 共享图像
Posted
技术标签:
【中文标题】如何使用 Facebook android 共享图像【英文标题】:How to Share Image using Facebook android 【发布时间】:2016-03-01 07:19:22 【问题描述】:我正在尝试通过 Facebook 共享图像和内容,但我正在尝试的是如果用户没有通过 Facebook 登录,并且用户单击共享按钮,那么首先它应该要求登录,但是当我运行我的代码时我没有收到错误,但屏幕上什么也没发生,它进入我的 else 部分,在 logcat 中显示 else 部分的消息
适配器
holder.sharefb.setOnClickListener(new OnClickListener()
@Override
public void onClick(View view)
if (strtextfb == null)
share();
);
方法
public void RequestData()
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback()
@Override
public void onCompleted(JSONObject object,GraphResponse response)
JSONObject json = response.getJSONObject();
try
if(json != null)
String text = "<b>Name :</b> "+json.getString("name")+"<br><br><b>Email :</b> "+json.getString("email")+"<br><br><b>Profile link :</b> "+json.getString("link");
// details_txt.setText(html.fromHtml(text));
// profile.setProfileId(json.getString("id"));
// System.out.println("FbId" + fbids);
catch (JSONException e)
e.printStackTrace();
);
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,email,picture");
request.setParameters(parameters);
request.executeAsync();
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
public void share()
if (AccessToken.getCurrentAccessToken() != null)
RequestData();
String pictureUrl = "https://upload.wikimedia.org/wikipedia/en/9/90/Bale_as_Batman.jpg";
Bundle params = new Bundle();
params.putString("name", "" + "Aditya");
params.putString("message", "" + "Nver Give Up");
params.putString("caption", "" + "Hi");
params.putString("description", "" + "Lie Cheat Steal");
params.putString("link", "" + "https://www.google.co.in/");
if (pictureUrl != "")
params.putString("picture", "" + pictureUrl);
/* make the API call */
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/feed",
params,
HttpMethod.POST,
new GraphRequest.Callback()
public void onCompleted(GraphResponse response)
/* handle the result */
Log.e("res", "" + response.getError());
).executeAsync();
else
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>()
@Override
public void onSuccess(LoginResult loginResult)
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
@Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse)
);
@Override
public void onCancel()
@Override
public void onError(FacebookException e)
);
System.out.println("else part");
【问题讨论】:
【参考方案1】:您需要在从适配器单击按钮时调用以下代码。
List<String> permissionNeeds = Arrays.asList("publish_actions");
manager = LoginManager.getInstance();
manager.logInWithPublishPermissions(this, permissionNeeds);
manager.registerCallback(callbackManager,
new FacebookCallback<LoginResult>()
@Override
public void onSuccess(LoginResult loginResult)
publishImage();
@Override
public void onCancel()
System.out.println("onCancel");
@Override
public void onError(FacebookException exception)
System.out.println("onError");
);
private void publishImage()
Bitmap image = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
//You need to get bitmap from any source.
SharePhoto photo = new SharePhoto.Builder().setBitmap(image)
.setCaption("Welcome To Facebook Photo Sharing on steroids!")
.build();
SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(
photo).build();
ShareApi.share(content, null);
Toast.makeText(this, "Succsesfully posted on your wall",
Toast.LENGTH_LONG).show();
【讨论】:
@Arth Tilva 你能帮我解决这个问题吗? ***.com/questions/34198028/…【参考方案2】:第一次尝试登录时,访问令牌为空。所以,它正在进行其他部分。在其他部分执行此操作,我认为它可能对您有用:
LoginManager.getInstance().registerCallback(mcallbackManager, new FacebookCallback<LoginResult>()
@Override
public void onSuccess(LoginResult loginResult)
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
@Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse)
);
);
【讨论】:
意外的CallbackManager,请使用提供的Factory。它给出了这个错误 好的,我在我的 oncreate callbackManager = CallbackManager.Factory.create();.. 并且错误已经消失了..但它仍然只显示其他部分消息 阅读我的第一行答案,将此代码放在其他部分 仅在其他部分【参考方案3】:first add
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
permissionNeeds = Arrays.asList("publish_actions");
manager = LoginManager.getInstance();
**on onCreate and wirte below code in send button lick event**
send.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
if (isOnline())
new WebView(MainActivity.this);
manager.logInWithPublishPermissions(MainActivity.this, permissionNeeds);
manager.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
@Override
public void onSuccess(LoginResult loginResult)
sharePhotoToFacebook();
@Override
public void onCancel()
System.out.println("onCancel");
@Override
public void onError(FacebookException exception)
exception.printStackTrace();
);
alertDialog.dismiss();
else
Toast.makeText(getBaseContext(), "Please check internet connection", Toast.LENGTH_SHORT).show();
);
private void sharePhotoToFacebook()
try
Log.i("Facebook Image", camera_pathname + "");
Bitmap image = BitmapFactory.decodeFile(camera_pathname);
//Bitmap image = decodeFile(camera_pathname);
image = getResizedBitmap(image);
//Bitmap image = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
SharePhoto photo = new SharePhoto.Builder()
.setBitmap(image)
.setCaption(et_hashtag.getText().toString())
.build();
SharePhotoContent content = new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
ShareApi.share(content, null);
Toast.makeText(getBaseContext(), "Shared successfully", Toast.LENGTH_SHORT).show();
catch (Exception e)
e.printStackTrace();
// manager.logOut();
【讨论】:
什么是发送..我只有一个听众可以分享 我的按钮在适配器中,我有 listview bhai以上是关于如何使用 Facebook android 共享图像的主要内容,如果未能解决你的问题,请参考以下文章
未安装应用程序(例如 Facebook)的 Android 共享
Android 如何在新的 sdk 3.0 中与 Facebook Open Graph 共享数据?