问题
I am trying to share Image and Content via Facebook,but what i am trying is if user is not logged in via Facebook,and user click on Share button then first it should ask for login,but when i run my code i am not getting error but nothing happen in screen and it goes in my else part and in logcat it display message of else part
Adapter
holder.sharefb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (strtextfb == null) {
share();
}
}
});
Methods
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:
You need to call below code on click of button from adapter.
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();
}
回答2:
First time when you try to log in, access token is null. So, it is going on else part. Do this in else part, I think so it may work for you :
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) {
}
});
}
});
回答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();
}
来源:https://stackoverflow.com/questions/33951442/how-to-share-image-using-facebook-android