1

I want to upload an image from an SD card on Facebook. An image can be posted on Facebook from a URL, but it's not getting posted if I'm passing the image from the SD card.

I am passing byteArray instead string for uploading image still I'm not getting the output. Can anyone please help me solve this issue?

My code:

    byte[] data = null;        
    params.putString(Facebook.TOKEN, facebook.getAccessToken());
    params.putString("link", "https://www.facebook.com/pages/My-Short-Sale-Score/242779072468511");
    params.putByteArray("picture",data);

    Facebook facebook = new Facebook("318633718220473");

    mAsyncRunner = new AsyncFacebookRunner(facebook);  
    mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);

   facebook.dialog(this, "stream.publish", params,

              new DialogListener() {
                   @Override
                   public void onComplete(Bundle values){} 

                   @Override
                   public void onFacebookError(FacebookError error) {}

                   @Override
                   public void onError(DialogError e) {}

                   @Override
                   public void onCancel() {}
              }
        );              
}

 public class SampleUploadListener extends BaseRequestListener {

        public void onComplete(final String response, final Object state) {
            try {
                // process the response here: (executed in background thread)
                Log.d("Facebook-Example", "Response: " + response.toString());
                JSONObject json = Util.parseJson(response);
                final String src = json.getString("src");

                // then post the processed result back to the UI thread
                // if we do not do this, an runtime exception will be generated
                // e.g. "CalledFromWrongThreadException: Only the original
                // thread that created a view hierarchy can touch its views."

            } catch (JSONException e) {
                Log.w("Facebook-Example", "JSON Error in response");
            } catch (FacebookError e) {
                Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
            }
        }

        @Override
        public void onFacebookError(FacebookError e, Object state) {    
        }
 }
Ry-
  • 218,210
  • 55
  • 464
  • 476
Shah Paneri
  • 729
  • 7
  • 28

2 Answers2

2

Create a file Object from Filepath on your SDcard like

File ff = new File("Your sdcard file path");
        bb = decode(ff);
    }       
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bb.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    Bundle params = new Bundle();
    params.putString("method", "photos.upload");
    params.putString("caption", msg);
    params.putByteArray("image", byteArray);
    try {

        facebook.request("me/feed", params, "POST");

    } catch (FileNotFoundException fileNotFoundException) {
        fileNotFoundException.printStackTrace();
    } catch (MalformedURLException malformedURLException) {
        malformedURLException.printStackTrace();
    } catch (IOException ioException) {
        ioException.printStackTrace();
    }
    AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
    mAsyncRunner.request(null, params, "POST", new SampleUploadListener(),
            null);

// code for decode method

private Bitmap decode(File f) {
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //The new size we want to scale to
        final int REQUIRED_SIZE=70;

        //Find the correct scale value. It should be the power of 2.
        int scale=1;
        while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
            scale*=2;

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}

    return null;
}
Akram
  • 7,548
  • 8
  • 45
  • 72
NARESH REDDY
  • 682
  • 4
  • 11
1

Try these Facebook open-source examples:

https://github.com/facebook/facebook-android-sdk

in this examples under hackbook for uploading photos to Facebook from sdcard!

(or)

Try this code for upload image to facebook from sdcard

byte[] data = null;
 try {
     FileInputStream fis = new FileInputStream(PATH_TO_FILE);
     Bitmap bi = BitmapFactory.decodeStream(fis);
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
     data = baos.toByteArray();  
  } catch (FileNotFoundException e) { 
     e.printStackTrace();
     Log.d("onCreate", "debug error  e = " + e.toString());
  }     

     Bundle params = new Bundle(); 
     params.putString("method", "photos.upload");  
     params.putByteArray("picture", data);

     Facebook facebook = new Facebook(FACEBOOK_APP_ID);
     AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
     mAsyncRunner.request(null, params, "POST", new RequestListener() {

        public void onMalformedURLException(MalformedURLException e, Object state) {
            Log.d("request RequestListener", "debug onMalformedURLException");
        }

        public void onIOException(IOException e, Object state) {
            Log.d("request RequestListener", "debug onIOException");
        }

        public void onFileNotFoundException(FileNotFoundException e, Object state) {
            Log.d("request RequestListener", "debug onFileNotFoundException");
        }

        public void onFacebookError(FacebookError e, Object state) {
            Log.d("request RequestListener", "debug onFacebookError");
        }

        public void onComplete(String response, Object state) {
             Log.d("request RequestListener", "debug onComplete");
        }
     }, null);

Note: You must set Permission for internet and Sdcard Reading manifest xml file on your project

Ry-
  • 218,210
  • 55
  • 464
  • 476
Dinesh
  • 6,500
  • 10
  • 42
  • 77
  • The link you have sent for github also displays image from URL not from sdcard.. public static final String HACK_ICON_URL = "http://www.facebookmobileweb.com/hackbook/img/facebook_icon_large.png"; – Shah Paneri Jun 18 '12 at 11:31
  • I am getting error 06-18 17:42:49.911: E/AndroidRuntime(8647): java.lang.OutOfMemoryError: bitmap size exceeds VM budget – Shah Paneri Jun 18 '12 at 12:13
  • You can fix the problem using opts.inSampleSize=2; or opts.inSampleSize=4 BitmapFactory.Options opts = new BitmapFactory.Options(); // opts.inJustDecodeBounds = true; opts.inSampleSize=2; Bitmap myBitmap = BitmapFactory.decodeFile(st_imagepath,opts); – Dinesh Jun 18 '12 at 12:15
  • if i write this code: Bitmap bitmap; ByteArrayOutputStream baos = new ByteArrayOutputStream(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 3; bitmap = BitmapFactory.decodeFile(filepath, options); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); data = baos.toByteArray(); baos.flush(); baos.close(); i am not getting VM budget size error but then Shutting down VM is coming in logcat. – Shah Paneri Jun 18 '12 at 12:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12692/discussion-between-dinesh-and-panu-parekh) – Dinesh Jun 18 '12 at 12:20
  • set options.inSampleSize = 2; (or) options.inSampleSize = 4; – Dinesh Jun 18 '12 at 12:23
  • if same error occurs try this post http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966 – Dinesh Jun 18 '12 at 12:34