1

Possible Duplicate:
How to post image to facebook wall using graph api

I am using Graph API to post on Facebook wall.

strpostimageurl=/mnt/sdcard/DCIM/mnt/sdcard/DCIM/Camera1354795516555.jpg

String res = UrltoValue.getValuefromUrl("https://graph.facebook.com/"+Login.facebookid+"/feed?access_token="+accesstoken+"&method="+"post"+"&message="+"hi"+"&link="+strpostimageurl);

I am getting Bad Request as a response.

Is it possible to give SD-Card path for Posting images? I am having only Bitmap of the object, so I created file with that Bitmap and I am using that.

And I need to post on multiple friends wall,so I am preferring to use Graph API.

Community
  • 1
  • 1
user1871951
  • 105
  • 3
  • 8

1 Answers1

0

You are getting the Bad Request error because you are passing the wrong values to the Facebook API.

First, if you are uploading an Image / Photo via a link or an URL, then you have to use the "source" tag and not the "link" tag as you have in your code.

Second, if you were to fix the above, I doubt the code would work seeing that your strpostimageurl does not actually have a valid URL.

Third, since it is not a valid URL, using the "source" tag would not be an option. You will need to use something like this:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmpImageGallery.compress(CompressFormat.JPEG, 100, baos);

Bundle postImgGallery = new Bundle();

// ADD THE PHOTO DATA TO THE BUNDLE
postImgGallery.putByteArray("photo", baos.toByteArray());

// ADD THE CAPTION FROM THE STRING finalStatusMessage TO THE BUNDLE
if (finalStatusMessage.equals(""))  {
    /*****  DO NOTHING HERE     *****/
} else {
    postImgGallery.putString("caption", finalStatusMessage);
}

mAsyncRunner.request("me/photos", postImgGallery, "POST", new PhotoUploadListener(), null);

Here,

  1. mAsyncRunner is an instance of the AsyncFacebookRunner Class that is part of the Facebook SDK.
  2. bmpImageGallery is an instance of a Bitmap in which the Image selected by the user is stored. It gets the Image selected by the User from the gallery using:

This is code runs on the OnClick event of a Button that opens the Gallery and lets the User select a Photo / Image from it:

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, reqcdGalleryImage);

Then, using the onActivityResult() method, the selected image is passed to the Bitmap instance bmpImageGallery used in the code earlier:

bmpImageGallery = MediaStore.Images.Media.getBitmap(this.getContentResolver(), targetURI);

EDIT:

Then try this (OnClick) :

Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");

File cameraFolder;

if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
    cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"give_a_folder_name/camera");
else
    cameraFolder= StatusUpdate.this.getCacheDir();
if(!cameraFolder.exists())
    cameraFolder.mkdirs();

File photo = new File(Environment.getExternalStorageDirectory(), "give_a_folder_name/camera/camera_snap.jpg");
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
initialURI = Uri.fromFile(photo);

startActivityForResult(getCameraImage, 2);

In this onActivityResult():

targetURI = initialURI;
getContentResolver().notifyChange(targetURI, null);

ContentResolver cr = getContentResolver();

try {

    // SET THE IMAGE FROM THE CAMERA TO THE IMAGEVIEW
    bmpImageCamera = android.provider.MediaStore.Images.Media.getBitmap(cr, targetURI);

    // SET THE IMAGE FROM THE GALLERY TO THE IMAGEVIEW
    imgvwSelectedImage.setImageBitmap(bmpImageCamera);

} catch (Exception e) {
    e.printStackTrace();
}

The targetURI and initialURI are Uri instances declared globally

Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • I am taking this image from camera and storing into sdcard.Can I use this path,to post in graph api.If not possible,how can I convert into valid one – user1871951 Dec 06 '12 at 13:07
  • /mnt/sdcard/DCIM/mnt/sdcard/DCIM/Camera1354795516555.jpg old one /mnt/sdcard/DCIM/mnt/sdcard/DCIM/Camera/1354795516555.jpg new one Is it right? – user1871951 Dec 06 '12 at 13:14
  • I am using surfaceview,and one more thing is why cant I add the path of sdcard to graph api source tag.I think I have missed "/" between camera and file name.And did I need to add any permission. I just want to know why it is not possible to add path of sdcard. – user1871951 Dec 06 '12 at 13:35