1

I already done text sharing using Facebook SDK. I am stuck at uploading images from SD card. Before parsing image to FB SDK, first I convert the image to bitmap then I supposed to share with Facebook. But this logic doesn't work. It never uploads images from SD card. Can anyone guide me on how to upload images from SD card? Thanks in advance.

Bitmap bi = BitmapFactory.decodeStream(fis);
Bitmap bi =BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() +"/Android/test.jpg", options);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
ozbek
  • 20,955
  • 5
  • 61
  • 84
RAAAAM
  • 3,378
  • 19
  • 59
  • 108

2 Answers2

1

try the below code and also check your bitmap should not null..

Bundle bundle = new Bundle();
bundle.putString("message", "post image.." );

byte[] data = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
<<your bitmap>>.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();

bundle.putString(Facebook.TOKEN, facebook.getAccessToken());
bundle.putString("method", "photos.upload");
bundle.putByteArray("picture", data);
bundle.putString("message", "post image.." );

mAsyncRunner.request(null,bundle,"POST",new RequestListener() {
    @Override
    public void onMalformedURLException(MalformedURLException e, Object state) {
      Log.d("request RequestListener", "debug onMalformedURLException");
    }
    @Override
    public void onIOException(IOException e, Object state) {
  Log.d("request RequestListener", "debug onIOException");
    }
    @Override
    public void onFileNotFoundException(FileNotFoundException e, Object state) {
  Log.d("request RequestListener", "debug onFileNotFoundException");
    }
    @Override
    public void onFacebookError(FacebookError e, Object state) {
  Log.d("request RequestListener", "debug onFacebookError");
    }
    @Override
    public void onComplete(String response, Object state) {
   Log.d("request RequestListener", "debug onComplete");
    }
}, null);
0

try this.

 private Facebook facebook = new Facebook("319*********897");
 byte[] byteArray;

get image from SD-Card and convert into byteArray.

then use this function for share image on facebook.

 public void shareOnFacebook() {
    // mDuckHuntView.pause();

    facebook.authorize(this, new String[] { "publish_stream",
            "user_photos", "publish_checkins", "publish_actions" },
            new DialogListener() {
                public void onComplete(Bundle values) {
                    try {

                        Bundle bundle = new Bundle();
                        bundle.putString(
                                "name",
                                "text here");
                        bundle.putByteArray("file", byteArray);
                        bundle.putByteArray(Facebook.TOKEN, values
                                .getString(Facebook.TOKEN).getBytes());
                        String response = facebook.request("me/photos",
                                bundle, "POST");

                        Log.d("UPDATE RESPONSE", "" + response);
                        JSONObject json = Util.parseJson(response);
                        if (!json.isNull("id")) {
                            // err("Facebook Image link submitted.");

                        } else {
                            // err("Facebook Error: " + response);
                        }

                    } catch (Exception e) {


                    } catch (FacebookError e) {

                    }
                }

                public void onFacebookError(FacebookError e) {

                }

                public void onError(DialogError e) {

                }

                public void onCancel() {

                }

            });
}

for this you need to use facebook sdk package.

Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
  • Thanks it works fine. Now i can able to share image from sdcard. – RAAAAM May 22 '13 at 07:20
  • A dialog is open and after login image is not posting.When i debug it. cursor not going to oncomplete. Can u share full code for login and sharing image on fb. or any hint to resolve my issue. – Akanksha Rathore Jan 21 '14 at 15:45