Android - How to update Status on Facebook attaching a picture - android

I'm using the Facebook's SDK for Android. I'm using the newStatusUpdateRequest() method to update my Status with a simple message, it works fine. But I don't find the way to attach a picture or an URL to the post.
private void postStatusUpdate() {
if (canPresentShareDialog) {
FacebookDialog shareDialog = createShareDialogBuilderForLink().build();
uiHelper.trackPendingDialogCall(shareDialog.present());
} else if (user != null && hasPublishPermission()) {
final String message = getString(R.string.status_update, user.getFirstName(), (new Date().toString()));
Request request = Request
.newStatusUpdateRequest(Session.getActiveSession(), message, place, tags, new Request.Callback() {
#Override
public void onCompleted(Response response) {
showPublishResult(message, response.getGraphObject(), response.getError());
}
});
request.executeAsync();
} else {
pendingAction = PendingAction.POST_STATUS_UPDATE;
}
}
I was reading also this link, with all the facebook API methods, but I can't find the way to post both in my Status: a message and a picture:
https://developers.facebook.com/docs/reference/android/3.0/class/Request/#newStatusUpdateRequest
Has anyone solved already a similar problem?
Update. I think to post a picture I should use the method:
public static Request newGraphPathRequest(Session session, String graphPath, Callback callback)
Where graphPath should be the link to the picture. But I don't know how to join both, message and picture in the same Request.
Thanks you very much for your time

Ok I solved it:
private void postStatusUpdate() {
if (canPresentShareDialog) {
FacebookDialog shareDialog = createShareDialogBuilderForLink().build();
uiHelper.trackPendingDialogCall(shareDialog.present());
} else if (user != null && hasPublishPermission()) {
Bitmap image = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon);
Request request = Request.newUploadPhotoRequest(Session.getActiveSession(), image, new Request.Callback() {
#Override
public void onCompleted(Response response) {
showPublishResult(getString(R.string.photo_post), response.getGraphObject(), response.getError());
}
});
final String message = getString(R.string.status_update, user.getFirstName(), (new Date().toString()));
Bundle params = request.getParameters();
params.putString("message", message);
request.executeAsync();
} else {
pendingAction = PendingAction.POST_STATUS_UPDATE;
}
}
If anyone has got a better solution, feel free to comment

Related

Posting image on Facebook timeline using Facebook SDK for Android

I have an Android app that posts user's status messages to her/his timeline on Facebook (see the code fragment which posts the message below), the problem is that I cannot post any status images like an emoji. I tried sending the image as a byte array (someone suggested that in another post) to no avail, the picture is rejected and if I post the URL of the image, Facebooks inserts the URL instead of the actual image. Is there a way to have the image posted as a real picture? I'm using Facebook SDK 4.11 for Android and Android Studio 2.1. Any hint will be welcome. Thank you
Here is code:
postParams.putString("caption", getResources().getString(R.string.app_name)); // text to post
postParams.putString("message", msg);
postParams.putString("link", "https://www.terra7.net");
if (imgUrl != null) {
//Bitmap bi = BitmapFactory.decodeResource(getResources(), imgId);
//ByteArrayOutputStream baos = new ByteArrayOutputStream();
//bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
//byte[] data = baos.toByteArray();
//postParams.putByteArray("picture", data); // image to post
postParams.putString("picture", imgUrl); // image to post
}
GraphRequest request = new GraphRequest(accessToken, "me/feed", postParams, HttpMethod.POST, new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse response) {
if (response.getError() != null) {
Toast.makeText(MoodFragment.this.getContext(), response.getError().getErrorMessage(), Toast.LENGTH_LONG).show();
}
}
});
request.executeAsync();
Try below code:
public void sharePhotoToFacebook() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
SharePhoto photo = new SharePhoto.Builder()
.setBitmap(bitmap)
.build();
SharePhotoContent content = new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Log.e("onSuccess ", "result " + result);
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
Log.e("onError ", "error " + error);
}
});
}
For using above code you need publish_actions permission you can use below code for get permission:
public void getPermission() {
List<String> permissionNeeds = Arrays.asList("publish_actions");// - See more at: http://simpledeveloper.com/how-to-share-an-image-on-facebook-in-android/#sthash.v7b5g7GA.dpuf
loginManager = LoginManager.getInstance();
loginManager.logInWithPublishPermissions(this, permissionNeeds);
loginManager.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) {
System.out.println("onError");
}
});
// - See more at: http://simpledeveloper.com/how-to-share-an-image-on-facebook-in-android/#sthash.v7b5g7GA.dpuf
}

How to execute graph-api Facebook Android SDK to upload photo and tag people?

How to execute graph-api Facebook Android SDK to upload photo and tag people?
On web there is a lot of confusion with facebook sdk for android.
My first approach
Bitmap img = bitmap;
if (img != null) {
Request request = Request.newUploadPhotoRequest(Session.getActiveSession(), img, uploadPhotoRequestCallback);
Bundle parameters = request.getParameters();
parameters.putString("message", "Winner #[" + uid1 + "] Second #[" + uid2 + "] Terzo #[" + uid3 + "]");
// add more params here
request.setParameters(parameters);
request.executeAsync();
}
but it doesn't tag people
https://developers.facebook.com/docs/opengraph/guides/tagging/
I summarized my question above because I posted 2 questions without response .
So I solved my problem and I am posting a solution to help another people of stackoverflow comunity.
To upload photo on facebook using facebook sdk 3.5:
To be sure to aver the right persmission "publish_actions","user_photos"
know that you have to upload photo before and tag people after.
To Upload photo on facebook
... //put update method where you need
Session session = Session.getActiveSession();
Bitmap img = .... //your bitmap
if (img != null) {
Request request = Request.newUploadPhotoRequest(Session.getActiveSession(), img, uploadPhotoRequestCallback);
Bundle parameters = request.getParameters();
parameters.putString("message", "my personal massage");
// add more params here
request.setParameters(parameters);
request.executeAsync();
}
...
private String idUploadResponse;
Request.Callback uploadPhotoRequestCallback = new Request.Callback() {
#Override
public void onCompleted(Response response) {
if (response.getError() != null) {
// post error
} else {
idUploadResponse= (String) response.getGraphObject().getProperty("id");
if (idUploadResponse!= null) {
String fbPhotoAddress = "https://www.facebook.com/photo.php?fbid=" + idUploadResponse;
tagPeople();
} else {
// error
}
}
}
};
idUploadResponse is the photo id on facebook.
with the idUploadResponse you can tag friends.
uid1 variable conteins the id of user (get from facebook).
public void tagPeople(){
Bundle params = new Bundle();
params.putString("to", uid1);
Session session = Session.getActiveSession();
final String requestId = idUploadResponse +"/tags";
Request request = new Request(session, requestId, params, HttpMethod.POST, new Request.Callback() {
public void onCompleted(Response response) {
GraphObject graphObject = response.getGraphObject();
FacebookRequestError error = response.getError();
if (error != null) {
Log.e("Error", error.getErrorMessage());
}
}
});
Request.executeAndWait(request);
}
To tag multi user
protected void tagPeople() {
Bundle params = new Bundle();
String multiTag= "[{'tag_uid':'"+uid1+"'} ,{'tag_uid':'"+uid2+"'}, {'tag_uid':'"+uid3+"'}]";
params.putString("tags", multiTag);
....
}

How to integrate "Like" and "Comment" feature using Android Facebook SDK?

I want to implement "Like" and "Comment" feature in my app. I used this code:
public static void like(String postID) {
String grapPath = String.format("%s/likes", postID);
Request request = new Request(Session.getActiveSession(), grapPath,
null, HttpMethod.POST, new Callback() {
#Override
public void onCompleted(Response response) {
Log.i(TAG, response.toString()+" Success!");
}
});
Request.executeBatchAsync(request);
}
public static void postComment(String comment, String postID) {
String grapPath = String.format("%s/comments", postID);
Bundle bundle = new Bundle();
bundle.putString("message", comment);
Request request = new Request(Session.getActiveSession(), grapPath,
bundle, HttpMethod.POST, new Callback() {
#Override
public void onCompleted(Response response) {
Log.i(TAG, "Success!");
}
});
Request.executeBatchAsync(request);
}
Hhow and where can I call these methods to make them work?
Please make sure the prerequisites are set up correctly. Specifically check out the middle of step 4 to make sure you generated your key hash correctly using your debug keystore.
Otherwise code below should help out
private boolean hasPublishPermission() {
Session session = Session.getActiveSession();
return session != null && session.getPermissions().contains("publish_actions");
}
private void postStatusUpdate() {
if (hasPublishPermission()) {
final String message = "Posting to facebook";
Request request = Request
.newStatusUpdateRequest(Session.getActiveSession(), message, place, tags, new Request.Callback() {
#Override
public void onCompleted(Response response) {
showPublishResult(message, response.getGraphObject(), response.getError());
}
});
request.executeAsync();
} else {
pendingAction = PendingAction.POST_STATUS_UPDATE;
}
}

Facebook post not visible even with public privacy

I'm using Facebook SDK 3 for android to share status in my wall .. I authorize the publication to be visible to all my friend , The post is well published but no one can see it expect me , even with privacy :public
private void postStatusUpdate() {
if (user != null && hasPublishPermission()) {
final String message = getString(R.string.status_update, user.getFirstName(), (new Date().toString()));
Request request = Request
.newStatusUpdateRequest(Session.getActiveSession(), message, new Request.Callback() {
#Override
public void onCompleted(Response response) {
showPublishResult(message, response.getGraphObject(), response.getError());
}
});
request.executeAsync();
} else {
pendingAction = PendingAction.POST_STATUS_UPDATE;
}
}
Change the standbox mode ( desactivate it )

Share link and text with Android Facebook SDK 3.0

Im trying to upgrade to the Facebook SDK 3.0 and have finally gotten everything to work with Request.newStatusUpdateRequest(). However my app shares/posts text along with a link. I have tried/looked into the following:
Request.newStatusUpdateRequest()
This does not seem to have any options for a Bundle or any other way to include a link and icon.
Request.newRestRequest()
Skipped this because I saw REST was being depreciated.
new WebDialog.FeedDialogBuilder(_activity, session, params).build().show();
This actually works pretty well but the resulting post does not seem to be linked to my Facebook App and I am not sure how this will effect my Facebook insights.
Request.newPostRequest()
From what I have read, this method seems to be the proper way. However, i cannot figure out where to get the GraphObject to pass in as one of the parameters.
What is the PROPPER way to post/share text, link and image to the user's wall? It seems to be Request.newPostRequest() so I will include the code I have for that.
Request request = Request.newPostRequest(session, "me/feed", ??graph_object??, new Request.Callback() {
#Override
public void onCompleted(Response response) {
showPublishResult("message", response.getGraphObject(), response.getError());
}
});
request.setParameters(params);
Request.executeBatchAsync(request);
But what really is a GraphObject? Where do i get the graph_object? The more I read from FB on GraphObject/OpenGraph/Graph API the more I get confused.
If I am heading down the wrong direction entirely, please tell me. If Request.newPostRequest is the propper way of doing this, please give me more information on the GraphObject param.
Finally managed to get everything I needed with the Facebook SDK 3.0 using the following:
Bundle params = new Bundle();
params.putString("caption", "caption");
params.putString("message", "message");
params.putString("link", "link_url");
params.putString("picture", "picture_url");
Request request = new Request(Session.getActiveSession(), "me/feed", params, HttpMethod.POST);
request.setCallback(new Request.Callback() {
#Override
public void onCompleted(Response response) {
if (response.getError() == null) {
// Tell the user success!
}
}
});
request.executeAsync();
I did by using this method.
See if this can help or not.
public static void publishFeedDialog(final Activity current, final String title,
final String caption, final String description, final String link,
final String pictureUrl) {
// start Facebook Login
Session.openActiveSession(current, true, new Session.StatusCallback() {
// callback when session changes state
#Override
public void call(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
Bundle params = new Bundle();
params.putString("name", title);
params.putString("caption", caption);
params.putString("description", description);
params.putString("link", link);
params.putString("picture", pictureUrl);
WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(
current, 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) {
ToastHelper.MakeShortText("Posted");
} else {
// User clicked the Cancel button
ToastHelper
.MakeShortText("Publish cancelled");
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
ToastHelper
.MakeShortText("Publish cancelled");
} else {
// Generic, ex: network error
ToastHelper
.MakeShortText("Error posting story");
}
}
}).build();
feedDialog.show();
}
}
});
To share page or link
Bundle params = new Bundle();
params.putString("link", "link_url");
Request request = new Request(Session.getActiveSession(), "me/feed", params, HttpMethod.POST);
request.setCallback(new Request.Callback() {
#Override
public void onCompleted(Response response) {
if (response.getError() == null) {
// Tell the user success!
}
}
});
request.executeAsync();
For more post parameters see me/feed on developer.facebook.com

Categories

Resources