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,
mAsyncRunner
is an instance of the AsyncFacebookRunner Class
that is part of the Facebook SDK.
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