I am taking a photo using an Android phone, and I want to post it in Facebook. My code:
private void SharePhotoFacebook(Bitmap bmp) {
FacebookSdk.sdkInitialize(FaceActivity.this);
// Create a callbackManager to handle the login responses.
callbackManager = CallbackManager.Factory.create();
shareDialog = new ShareDialog(this);
SharePhoto photo = new SharePhoto.Builder()
.setBitmap(bmp)
.build();
if(ShareDialog.canShow(SharePhotoContent.class)){
SharePhotoContent content = new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
shareDialog.show(content);
}
// this part is optional
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Log.i("David", "Éxito");
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
Log.i("David", "Error");
}
});
}
But no dialog is showing, and nothing is posted in Facebook. I have implemented this code in the same Activity I take photo (FaceActivity), and is the Activity declared in Facebook Developer page of the app. What is wrong?
I can see like something tries to show (a grey "flash" that appears in the screen) but nothing shows.
EDIT: Im taking photo this way:
makePhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCameraSource.takePicture(null, new CameraSource.PictureCallback() {
#Override
public void onPictureTaken(#NonNull byte[] bytes) {
Log.i("David", String.valueOf(bytes.length));
bmp= BitmapFactory.decodeByteArray(bytes,0,bytes.length);
ShowShareDialog();
//SharePhotoFacebook(bmp);
}
});
}
});
I just realized I have this in my debug:
Unsupported get request. Object with ID 'XXXXXXX' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api [extra]:
Related
i am not able to get publish_actions permission of facebook sdk4+ i am facing problem and i found there is no proper example available. if any one has the solution do post here. i found that old version solution are shown by everyone but did not found solution for sdk4+ .
LoginManager loginManager = LoginManager.getInstance();
loginManager.logInWithPublishPermissions(this, permissionNeeds);
Log.e("publish_action", "asking_for_permission");
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>()
{
#Override
public void onSuccess(LoginResult loginResult)
{
if (AccessToken.getCurrentAccessToken().getPermissions().contains("publish_actions"))
{
Log.e("publish_action", "permission_activated");
Bitmap image = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
try {
Log.e("publish_action", "permission_activated_inside_try");
SharePhoto photo = new SharePhoto.Builder()
.setBitmap(image)
.setCaption("A post from Android code")
.build();
SharePhotoContent content = new SharePhotoContent.Builder()
.addPhoto(photo)
.addPhoto(photo)
.addPhoto(photo)
.build();
ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Log.e("successfull", "photo_uploaded");
}
#Override
public void onCancel() {
Log.e("canceled", "photo_uploaded_cancled");
}
#Override
public void onError(FacebookException error) {
Log.e("error", "photo_upload_error");
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
#Override
public void onCancel() {
System.out.println("onCancel");
Log.e("publish_action", "cancel");
}
#Override
public void onError(FacebookException exception) {
Log.e("publish_action", "error = " + exception.getMessage());
System.out.println("onError");
}
});
I had the same problem, solution was
if (!AccessToken.getCurrentAccessToken().getPermissions().contains("PERMISSION_THAT_YOU_NEED")) {
LoginManager.getInstance().logInWithPublishPermissions((Activity) viewInterface.getContext(), Arrays.asList("PERMISSION_THAT_YOU_NEED"));
return;
You need to return after using logInWithPublishPermissions(), because user may not allow you to use it, but your code will continue to run. And if user provide access to this permission, code will complete successfully next time.
Try this, hope it help to you.
i found this link you must try this url this is a perfect demo for publish_actions permission to facebokk it was helpful for me.
I want to share only text via facebook sdk. I researched more time but generally use ShareLinkContent class. Its working but i dont want to add link only post text to user wall via to ShareButton. How i can do?
Code:
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentDescription(Html.fromHtml(shareText).toString())
.setContentUrl(Uri.parse("http://developers.facebook.com/android"))
.build();
ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Toast.makeText(activity, "Success..", Toast.LENGTH_LONG).show();
}
#Override
public void onCancel() {
Toast.makeText(activity, "Cancel..", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FacebookException e) {}
});
shareButton.setFragment(this);
shareButton.setShareContent(content);
FacebookSDK: 4.11.0
When we post image on facebook by my android app it's post successfully but when we click on that post on Facebook app (on mobile devices) there is a toast appear "There's a problem opening this app." but when open in WEB and click on that posted image it'll redirect to shared link successfully.
I have use this code to share post on facebook.
GraphRequest request = GraphRequest.newPostRequest(AccessToken.getCurrentAccessToken(),
"me/feed", null, new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse response) {
Log.i(TAG, response.toString());
//checkPostStatusAndEnableButton();
}
});
Bundle postParams = request.getParameters();
postParams.putString("link",post_url);
postParams.putString("caption", caption);
request.setParameters(postParams);
request.executeAsync();
Is we have use some other action for url for mobile devices?
Try this on :
private static List<String> PERMISSIONS = Arrays.asList("public_profile","user_photos","user_videos", "email","user_likes","user_posts",
"user_hometown", "user_location","user_about_me","user_birthday",
"user_friends","user_relationship_details");
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.i(TAG,"onSuccess registerCallback");
}
#Override
public void onCancel() {
// App code
Log.i(TAG,"onCancel registerCallback");
}
#Override
public void onError(FacebookException exception) {
// App code
Log.i(TAG,"onError registerCallback");
}
});
Then call this share link method :
private void shareLink() {
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("https://developers.facebook.com"))
.build();
ShareDialog shareDialog = new ShareDialog(this);
shareDialog.show(content, ShareDialog.Mode.AUTOMATIC);
}
When I try to implement the sharing of links for the app links using the ShareDialog using ShareLinkContent, the image and title are not shown correctly when the actually the post is shared.
String url = "https://fb.me/" + id; //Id is generated by app links hosting api
ShareDialog shareDialog = new ShareDialog(activity);
if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent.Builder linkContentBuilder = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse(url))
.setContentTitle(contentTitle)
.setContentDescription(contentDescription);
if (imageUrl != null) {
linkContentBuilder.setImageUrl(Uri.parse(imageUrl));
}
ShareLinkContent linkContent = linkContentBuilder.build();
shareDialog.setShouldFailOnDataError(true);
shareDialog.registerCallback(activity.callbackManager,
new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Log.d(TAG, "Facebook sharing successful");
}
#Override
public void onCancel() {
Log.d(TAG, "Facebook sharing cancelled");
}
#Override
public void onError(FacebookException e) {
Log.d(TAG, "Facebook sharing error");
Log.d(TAG, e.getMessage());
}
});
shareDialog.show(linkContent);
}
But the sharing looks like below:
Facebook sharing
Can someone help me out?
I want to share a picture on Facebook with a description but I only can post an image without description. How can I add a message/description to the photo?
The code I am using to share:
SharePhoto photo = new SharePhoto.Builder().setBitmap(shareBitmap).build();
ArrayList<SharePhoto> photos = new ArrayList<>();
photos.add(photo);
SharePhotoContent.Builder shareContentBuilder = new SharePhotoContent.Builder();
shareContentBuilder.addPhotos(photos);
SharePhotoContent sharePhotoContent = shareContentBuilder.build();
ShareApi.share(sharePhotoContent, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
});
Found the solution. I had to add .setCaption("Caption") to the SharePhoto Builder.