Android post image on facebook wall - android

I have tried many code to send the image on facebook wall but it is not working.
I am using this code
Bitmap bitmap = BitmapFactory.decodeResource(shareDialogContext.getResources(),R.drawable.splash);
byte[] byetArray = convertBitmapToByteArray(shareDialogContext,bitmap);
params.putByteArray("picture",byetArray);
objFacebook.request(profileID + "me/photos", params, "POST");
public byte[] convertBitmapToByteArray(Context context, Bitmap bitmap) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(bitmap.getWidth() *
bitmap.getHeight());
bitmap.compress(CompressFormat.PNG, 100, buffer);
return buffer.toByteArray();
}
Please suggest any useful code.

Doesn't hackbook example do exactly what you want?
Bundle params = new Bundle();
try {
params.putByteArray("photo",
Utility.scaleImage(getApplicationContext(), photoUri));
} catch (IOException e) {
e.printStackTrace();
}
params.putString("caption", "FbAPIs Sample App photo upload");
Utility.mAsyncRunner.request("me/photos", params, "POST",
new PhotoUploadListener(), null);

This may help you:
Bundle params = new Bundle();
try
{
params.putByteArray("photo",Utility.scaleImage(getApplicationContext(), photoUri));
}
catch (IOException e)
{
e.printStackTrace();
}
params.putString("caption", "Through My Android Application");
Utility.mAsyncRunner.request("me/photos", params, "POST",new PhotoUploadListener(), null);
Possible duplicate : Android - Upload photo to Facebook with Facebook Android SDK

Related

Wall post not display on Facebook wall from android application

it might be duplicate question so sorry please consider it as my problem .
i am facing problem image posting on facebook wall from android application but image not display on wall it is visible inside Photos-----> your photos section but i want this images should display on user time line or wall please help me .
my code is
Bundle params = new Bundle();
// Inflate Edit text for Facebook Message
params.putString("method", "photos.upload");
params.putString(Facebook.TOKEN, facebook.getAccessToken());
params.putString("app_id", mAPP_ID); // I've tried with/without this,
// same result
params.putString("message", message);
Bitmap b = BitmapFactory
.decodeResource(getResources(), R.drawable.test);
Log.i("Bitmap Image is here", "Bitmap" + b);
if (b != null) {
Log.i("Bitmap", "value");
} else {
Log.i("Bitmap", "null");
}
byte[] data = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
if (data != null) {
params.putByteArray("picture", data);
Log.i("final", "value" + message);
params.putString("access_token", facebook.getAccessToken());
try {
facebook.request("me");
facebook.request("me/photos", params, "POST");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I faced similar issue some time ago: FB combines all photo uploads into single post on your timeline when you upload more than one photo during a short period of time. Also, it looks like you do not provide custom photo message and privacy settings, so your code can be significantly simplified (I assume you are using the latest FB android sdk, have the session opened, and already requested publish_stream permission):
Bitmap image2Upload = ...;
Request uploadRequest = Request.newUploadPhotoRequest(Session.getActiveSession(), image2Upload, null);
String photoId = null;
String postId = null;
try {
Response uploadResponse = Request.executeAndWait(uploadRequest);
JSONObject json = uploadResponse.getGraphObject().getInnerJSONObject();
photoId = json.getString("id");
postId = json.optString("post_id");
} catch(Throwable t) {
Log.e(TAG, "Photo upload request failed!", t);
}

{"error":{"message":"(#324) Requires upload file","type":"OAuthException","code":324}}

private void postToWall(String msg) {
Bundle parameters = new Bundle();
// parameters.putString("method", "stream.publish");
JSONObject attachment = new JSONObject();
try {
byte[] data = null;
Bitmap bi = BitmapFactory.decodeResource(getResources(),
R.drawable.bluerib);
// Bitmap bi =
// BitmapFactory.decodeFile("http://demos.com/LangGuage/medal_1.png");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 30, baos);
data = baos.toByteArray();
// Bundle params = new Bundle();
// params.putByteArray("picture", data);
attachment.put("type", "image");
// attachment.put("picture", data);
attachment.put("name", "LangGuage");
attachment.put("message", msg);
attachment.put("src", "http://demos.com/LangGuage/medal_1.png");
attachment.put("href", "http://www.abc.com");
// attachment.put("href",
// "http://hwsdemos.com/LangGuage/medal_1.png");
parameters.putString(Facebook.TOKEN, facebook.getAccessToken());
parameters.putString("attachment", attachment.toString());
String response = facebook.request("me/photos", parameters, "POST");
System.out.println("----responce" + response);
if (response.contains("Duplicate status message")) {
progressHandler.sendEmptyMessage(1);
resp = 1;
} else if (response == null || response.equals("")
|| response.equals("false") || response.contains("error")) {
Log.d("error", "error response");
} else {
progressHandler.sendEmptyMessage(0);
resp = 0;
}
} catch (Exception e) {
Log.e(TAG, "Posting fail");
e.printStackTrace();
// finish();
}
}
My app is supporting single sign-in for facebook. I don't want to use dialogs.
Whenever I try to post an image and message together in a attachment I catch:
{"error":{"message":"(#324) Requires upload file","type":"OAuthException","code":324}}
See the docs for a photos graph POST request here https://developers.facebook.com/docs/reference/api/user/#photos
Your image data should go into the "source" field, and the only other available fields are "message", "place' and "no_story". The "src", "name", "href" are not valid fields.

Android unable to attach image to Facebook,Twitter,Email via my app.Please Suggest

I am making a photo gallery app in which I try to load images from asset folder. I'm successful in doing this.
Now I want to share the image(single not multiple) via email, facebook and twitter.
This is where my problem starts.
I'm able to post a plain message via my app but I'm unable to post my image or attach my image with it.
I don't know how to do this. I tried many tutorials but to no help.
You have to upload image first somewhere on the internet, so the Facebook can download it.
here you go it works for me:
final Session session = Session.getActiveSession();
if (session != null){
// check publish permissions here
// if there is no permission request it and return;
//else if there is permission
logMessage("Has permission go on");
final Bundle postParams = new Bundle();
if(File("yourImagePath").exists())
{
byte[] data = null;
File screenShotFile = new File("yourImagePath");
logMessage("getting screenShot here");
FileInputStream fis = new FileInputStream(screenShotFile);
Bitmap bitmap = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
postParams.putByteArray("picture", data);
logMessage("added pic as byte array to params");
}
postParams.putString("name", "Post name");
postParams.putString("caption", "caption");
postParams.putString("description", "desc");
postParams.putString("link", "http://www....");
final Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response)
{
FacebookRequestError error;
try {
error = response.getError();
if (error != null)
{
makeToast(error.getErrorMessage());
logMessage(error.getErrorMessage());
}
else
{
// no errors so delete image here
if(screenShotFile != null)
{
screenShotFile.delete();
logMessage("deleted");
screenShotFile = null;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
logMessage(response.toString());
}
};
Request request = new Request(session, "me/photos", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
logMessage("made feed request check");
}

Upload photo to FB from Android : No Error but no file uploaded to facebook

Here is the code for uploading picture into my facebook :
Bundle parameters = new Bundle();
parameters.putString("message", msg);
byte[] imgData = getImage("http://bandungraos.in/wp-content/resto/1/gallery/kepiting1.jpg");
parameters.putByteArray("picture", imgData);
if (imgData != null) {
try {
String response = facebook.request("me/photos", parameters,"POST");
System.out.println(response);
} catch (IOException e) {
e.printStackTrace();
}
}
.....
private byte[] getImage(String url) {
try {
URL imgUrl = new URL(url);
HttpURLConnection cn = (HttpURLConnection) imgUrl.openConnection();
cn.setDoInput(true);
cn.connect();
int length = cn.getContentLength();
byte[] imgData = new byte[length];
InputStream is = cn.getInputStream();
is.read(imgData);
return imgData;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
I checked that either access token or imgData is not null
There is no error but I can't find the image in my facebook.
Thanks in advance
According to the User object documentation about the Photos connection the image parameter is named "source" and not "picture", have you tried:
Bundle parameters = new Bundle();
parameters.putString("message", msg);
parameters.putByteArray("source", getImage("..."));
I had the same problem and i used this code `
private void upload_FB(Bitmap photo2) {
// TODO Auto-generated method stub
Calendar c = Calendar.getInstance();
String name = c.getTime().toString();
AsyncFacebookRunner fruner = new AsyncFacebookRunner(facebook);
Log.d("adr", mCurrentPhotoPath);
if(photo2!=null && mCurrentPhotoPath!=null){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo2.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] bMapArray = baos.toByteArray();
Bundle params = new Bundle();
params.putByteArray("photo",bMapArray);
params.putString("caption", name);
fruner.request("me/photos",params,"POST",new PhotoUploadListener(),null);
}else
Toast.makeText(ctx, "ERROR", Toast.LENGTH_LONG).show();
}
don't forget to add premision photo_upload
`

Uploading a video or an image to facebook using Android SDK

I wonder if someone know how to upload a video on my wall on facebook using android sdk
I searched a lot but no code works for me.
I tried the facebook android sdk example which called "Hackbook" for uploading an image but I didn't found any detailed tutorial about uploading videos using Android Sdk!
So if some one know how to do that by a snippet of code or something like that, it will be very nice.
Thanks guys.
byte[] data = null;
String dataPath = "/mnt/sdcard/KaraokeVideos/myvideo.3gp";
String dataMsg = "Your video description here.";
Bundle param;
facebook = new Facebook(FB_APP_ID);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
InputStream is = null;
try {
is = new FileInputStream(dataPath);
data = readBytes(is);
param = new Bundle();
param.putString("message", dataMsg);
param.putByteArray("video", data);
mAsyncRunner.request("me/videos", param, "POST", new fbRequestListener(), null);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
private void uploadVideo() {
try {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
String title = "My titles";
String description = "My description";
JSONObject jsonObject = new JSONObject();
jsonObject.put("value", "EVERYONE");
byte[] data = readBytes(reversedPath);
GraphRequest request = GraphRequest.newPostRequest(accessToken, "me/videos", null, this);
Bundle params = request.getParameters();
params.putByteArray("video.mov", data);
params.putString("title", title);
params.putString("privacy", jsonObject.toString());
params.putString("description", description);
params.putInt("file_size", data.length);
request.setParameters(params);
request.executeAsync();
progressBarHorizonatl.setVisibility(View.VISIBLE);
} catch (JSONException | IOException e) {
e.printStackTrace();}
}}
public byte[] readBytes(String dataPath) throws IOException {
InputStream inputStream = new FileInputStream(dataPath);
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}

Categories

Resources