Android: Sharing images via Facebook API - android

I am trying to share an image via the android facebook API to facebook.
Sharing text and links works fine, uploading an image, too.
I found out, that I only can post images, if they are online or in the photoalbum of the user.
Bundle bundle = new Bundle();
bundle.putParcelable("picture", b);
Request request = new Request(session, "me/photos", bundle,
HttpMethod.POST, new Request.Callback() {
#Override
public void onCompleted(Response response) {
String imageid = (String) response.getGraphObject()
.getProperty("id");
share(session, text, imageid);
}
});
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
imageid is a number like "123456789908765345678"
After the execution, "share" is called:
Bundle bundle = new Bundle();
bundle.putString("caption", "some text");
bundle.putString("description", "Imageid:"+imageid);
bundle.putString("link",
"https://link.de");
bundle.putString("name", "some text");
bundle.putString("place", imageid); //It doesn't work for me
new WebDialog.FeedDialogBuilder(this, session, bundle).build().show();
I don't know what I am doing wrong. I simply want so share this image.
Help please :)

You have to make another request with the id of the resource in order to get the url of the image you are trying to reference in the share dialog.
So you should do something like this:
Callback callback = new Callback() {
#Override
public void onCompleted(Response response) {
if (response.getGraphObject() != null) {
String imageUrl = (String) response.getGraphObject().getProperty("picture");
// call to your sharing method
share(session, text, imageUrl);
}
}
};
Request request = new Request(Session.getActiveSession(), imageid, null, HttpMethod.GET, callback);
request.executeAsync();

You can upload the photo to facebook album using
com.facebook.Request fbRequest = com.facebook.Request.newUploadPhotoRequest(session, image, callback);
fbRequest.executeAsync();

Related

Android facebook upload photo on a feed page

Hi guys I am trying to upload a photo to a page on Facebook. This is the page i am trying to post the photo https://www.facebook.com/manegedagen/.
I have been using the following code :
public static void postImageOnWall(Bitmap pBitmap) {
Bundle bundle =new Bundle();
bundle.putString("method", "photos.upload");
bundle.putString("picture","http://www.demos.com/LangGuage/medal_1.png");
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/2091172661108128/feed",
null,
HttpMethod.POST,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
Log.e("", "");
}
}
).executeAsync();
}
But i am getting an error message: OAuthException: errorMessage: Invalid parameters. I would really appreciate if i can get any guide or help.
Try with parameters not as null
see the selected answer from this reference:
https://stackoverflow.com/a/29770773/6011938

How can I upload photo to facebook album from android app using following graph API?

I am new to android development. I am trying to post photo from an android app to user's facebook album. I added following code after I logged in the user with permissions "user_friends"
new GraphRequest(
MainFragment.mAccessToken.getCurrentAccessToken(),
"/me/photos",
params,
HttpMethod.POST,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
}
}
// This is my code for user login at app start
post = (Button)view.findViewById(R.id.button1);
mLoginManager = LoginManager.getInstance();
mLoginManager.registerCallback(mCallbackManager, mCallback);
mLoginManager.logInWithReadPermissions(this, Arrays.asList("user_friends")); //user_friends // publish_actions
Please guide me
I did a mistake .. Firstly, my question was incomplete .. I have found the issue .. Following seems to be working.
MainFragment.mLoginManager.logInWithPublishPermissions(this, Arrays.asList("publish_actions"));
Bundle params = new Bundle();
params.putString("url", "http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg");
/* make the API call /
new GraphRequest(
MainFragment.mAccessToken.getCurrentAccessToken(),
"/me/photos",
params,
HttpMethod.POST,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/ handle the result */
Log.d("MainFragment:", "onCompleted mAccessToken = " + MainFragment.mAccessToken.getCurrentAccessToken());
}
}
).executeAsync();

Post image from android to facebook page

I have successfully post a feed in facebook page form the graph api.
try {
resObj.put("message","feed from android");
//resObj.put("object_attachment",bitmap);
} catch (JSONException e) {
e.printStackTrace();
}
GraphRequest request = GraphRequest.newPostRequest(
AccessToken.getCurrentAccessToken(),"363453267193844/photos",resObj,
new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
Log.i(TAG,"post page response::"+graphResponse);
}
}
);
request.executeAsync();
But, I'm unable to post image into facebook page. The problem is I'm unable to find the key for image attachment in Json data posted in Graph Api.
The failed response from facebook is
{Response: responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 324, errorType: OAuthException, errorMessage: (#324) Requires upload file}}
Finally, finally, I was able to post an image into facebook page. This is how I did to post an photo.
Bundle bundle=new Bundle();
bundle.putByteArray("object_attachment",byteArray);// object attachment must be either byteArray or bitmap image
bundle.putString("message","some message here");
GraphRequest graphRequest=new GraphRequest(AccessToken.getCurrentAccessToken(),
"{page_id}/photos",
bundle,
HttpMethod.POST,
new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse graphResponse) {
Log.i("post page response::" + graphResponse);
}
);
graphRequest.executeAsync();
1.) Make sure you a page access token with publish_pages permission that can be used to publish new photos.
2.) From the docs . Note that you dont have a "/" before pageid in your call.
There are two separate ways of publishing photos to Facebook:
1: Attach the photo as multipart/form-data. The name of the object
doesn't matter, but historically people have used source as the
parameter name for the photo. How this works depends on the SDK you
happen to be using to do the post.
2: Use a photo that is already on the internet by publishing using the
url parameter:
Bundle params = new Bundle();
params.putString("url", "{image-url}");
/* make the API call */
new Request(
session,
"/{page-id}/photos",
params,
HttpMethod.POST,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
}
}
).executeAsync();
There is no way to publish more then one photo in the same graph API
call.
3.) Example ==>
Try it like this i.e post byteArrayStream of your photo
postParams = new Bundle();
postParams.putString("message", "feed from android");
postParams.putBoolean("published",true);
String pageID = "363453267193844";
//Post to the page as the page user
ByteArrayOutputStream stream = new ByteArrayOutputStream();
<YOURBITMAPPHOTOHANDLE>.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
postParams.putByteArray("source", byteArray);
postParams.putString("access_token", "your_page_access_token");
/* make the API call */
new Request(
sessionInstance,
"/" + pageID + "/photos",
postParams,
HttpMethod.POST,
new Request.Callback() {
public void onCompleted(Response response) {
//An error occurred during posting to facebook
FacebookRequestError error = response.getError();
if (error != null) {
isPostingError = true;
postingErrorMessage = error.getErrorUserMessage();
} else {
isPostingError = false;
}
}
}
). executeAsync();

access facebook page information

I want to get facebook page about, description, location, basic info etc. I have got the access token for page. I am getting likes of page like this:
public void getLikes (String id,final TextView likes,final int position)
{
Session session = Session.getActiveSession();
Bundle params = new Bundle();
params.putString("id", id);
params.putString("fields", "likes");
Request request = new Request(session, "search", params, HttpMethod.GET, new Request.Callback() {
#Override
public void onCompleted(Response response) {
try {
JSONObject res = response.getGraphObject().getInnerJSONObject().getJSONArray("data").getJSONObject(0);
if (res.length()>1){
likes.setText(String.valueOf(res.getInt("likes")));
Preferences.data.getData().get(position).setLikes(String.valueOf(res.getInt("likes")));
}
else {
Preferences.data.getData().get(position).setLikes("No Likes");
likes.setText("0");
}
} catch (Exception e) {
}
}
});
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
If I add more parameters in bundle, I get the value of last parameter added, what should I do. Facebook's documentation about graph api is not much clear.
This isn't an issue specific to the API, you're just only using one field of the response (.getJSONObject(0))
if you request multiple fields you need to look at each of them, currently you're only fetching the first

Android post picture to Facebook public page wall

I'm currently able to post to a public page wall using:
JSONObject json = new JSONObject();
json.put("message", "I'm on your wall");
Request req = Request.newPostRequest(getSession(), "PowerCardSoftware/feed", GraphObject.Factory.create(json), new Callback() {
#Override
public void onCompleted(Response response) {
if(response.getError() != null)
Log.e("FRAGACTIVITY", response.getError().toString());
Toast.makeText(getBaseContext(), "I hacked your facebook!", Toast.LENGTH_SHORT).show();
}
});
Request.executeBatchAsync(req);
I would like to post a picture the user takes onto the public wall as well. I've tried using a Bundle instead of a JSONObject and using each of these lines:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
postPhoto.compress(CompressFormat.JPEG, 100, baos);
params.putByteArray("picture", baos.toByteArray());
params.putByteArray("source", baos.toByteArray());
They both give me an error like this - errorMessage: (#100) picture URL is not properly formatted
Anyone know how to post a photo onto someone else's facebook wall without using deprecated functions/Objects in the facebook sdk?
This is my code to upload a photo stored locally on the phone:
Request request6 = Request.newUploadPhotoRequest(
session,
((BitmapDrawable) getResources().getDrawable(
R.drawable.picture)).getBitmap(), callback6);
RequestAsyncTask task6 = new RequestAsyncTask(request6);
task6.execute();
This is to upload on your own wall. Reason why there is no option to choose another recipient is due to the breaking changes in February that will disable posting to other people's wall.
See my earlier answer.
EDIT:
what is the best way to upload a photo that will show up on a place's wall with a photo and message?
Can you try this and see if this works?
Bundle parameters = new Bundle();
parameters.putParcelable("picture", YOUR_BITMAP_HERE);
parameters.putString("message", "my message for the page");
return new Request(session, "PowerCardSoftware/feed", parameters, HttpMethod.POST, callback);
Can I add a message using newUploadPhotoRequest()?
No, to add a message with your photo, you won't be using newUploadPhotoRequest. If you dig into the source, its just a wrapper of a Request, so do the same as the method, but add an additional parameter, message, with the message you want, and execute it. I haven't personally verified it but it should work. Let me know if it doesn't.
This is my solution.
I referenced Jesse Chen's answer and made some modifications.
Bitmap image = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "demo.jpg");
Bundle parameters = new Bundle();
parameters.putParcelable("source", image);
parameters.putString("message", "my message for the page");
Request request = new Request(Session.getActiveSession(), "me/photos", parameters, HttpMethod.POST, new Request.Callback() {
#Override
public void onCompleted(Response response) {
showPublishResult(mainActivity.getString(R.string.photo_post), response.getGraphObject(), response.getError());
}
});
request.executeAsync();
You can check facebookSDK project /tests folders and search keywords newPostRequest.
the sample code is as below
GraphObject statusUpdate = GraphObject.Factory.create();
String message = "message";
statusUpdate.setProperty("message", message);
statusUpdate.setProperty("link", "http://stackoverflow.com/questions/14129546/android-post-picture-to-facebook-public-page-wall#");
Request request = Request.newPostRequest(Session.getActiveSession(), "me/feed", statusUpdate, new Callback() {
#Override
public void onCompleted(Response response) {
}
});
request.executeAsync();

Categories

Resources