Slacker616
Slacker616

Reputation: 855

Posting picture on a friend's wall?

Is it possible to post a picture in a friend's wall? I'm using this code :

Bundle postdata = new Bundle();
postdata.putString("message", msg);
postdata.putByteArray("picture", pic);
// this code, i found it in another questions but it doesn't
// seem to help much:
postdata.putString("target_id", usr);
// then i  use the Asynchronous request.
mAsyncRunner.request(usr+"/feed", postdata, "POST",new WallRequestListener(), null);

where pic is the picture as a byteArray and usr is the friend's ID. It just shows the message on the friend's wall but no picture at all.

Upvotes: 1

Views: 692

Answers (2)

Siddharth Lele
Siddharth Lele

Reputation: 27748

This is what I use in my app. And it works.

protected void uploadPhoto() {
    if (!Utility.mFacebook.isSessionValid()) {
        Util.showAlert(this, "Warning", "You must first log in.");
    } else {
        if (targetUri != null) {
            Bundle params = new Bundle();
            try {
                params.putByteArray("photo", Utility.scaleImage(getApplicationContext(), targetUri));
            } catch (Exception e) {
                e.printStackTrace();
            }
            params.putString("caption", txtPhotoCaption.getText().toString());
            Utility.mAsyncRunner.request( "replace this with the users ID"+ "/photos&access_token=" + Utility.mFacebook.getAccessToken(), 
                    params, "POST", new PhotoUploadListener(), null);
            txtPhotoCaption.setText("");
            Toast.makeText(getApplicationContext(), "Photo uploaded successfully", Toast.LENGTH_SHORT).show();
            this.finish();
        }
    }
}

Please note, that I am using the same method as used in the sample app. Without getting into the pros or cons of scaling or not scaling the image. The "caption" attribute is being pulled from an EditText. And the actual image is in the field "targetUri", which is declared as a global variable. It's fetching the image using the default Gallery on the device. For the sake of completeness, this is the code to grab the image from the default Gallery app. (this prompts the user to select the app)

btnAddPhoto.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 0);
            }
        });

And it is processed in the onActivityResult() here:

NOTE: I am also setting the selected image in an ImageView as a preview to the logged in user

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        Bitmap bitmap = null;
        if (resultCode == RESULT_OK)    {
            targetUri = data.getData();
            Log.e("IMG URI", targetUri.toString());
            try {
                bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
                try {
                    Utility.scaleImage(getApplicationContext(), targetUri);
                    imgDisplaySelectedImage.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

I have again tested an upload to a friends wall before I posted the code here as an answer. And it really works. Hope this helps you as well.

EDIT:

As mentioned in the latest comment, using Intent.ACTION_GET_CONTENT did not play nice with the preview image display under the onActivityResult(). Modifying the code a little worked.

What I find strange is the fact that in the "Complete action using" popup, I did not get the Astro File Manager nor the Gallery on my Galaxy S. It gives me File Manager (off the market, not the stock) and Quickoffice Pro. That being said, selections from either the two still works and photos still upload.

Upvotes: 1

Nitzan Tomer
Nitzan Tomer

Reputation: 164397

According to the documentation the name of the parameter for the picture is not "picture" but "source".

So your code should be:

Bundle postdata = new Bundle();

postdata.putString("message", msg);
postdata.putByteArray("source", pic);

mAsyncRunner.request(usr + "/feed", postdata, "POST", new WallRequestListener(), null);

Upvotes: 0

Related Questions