简体   繁体   中英

Upload image to FaceBook from SDCard

With Android 2.1 how can I upload an image on to a Facebook wall? The images are store in SDCard.

You should use Facebook API http://developers.facebook.com/docs/guides/mobile/#android . And then use this code:

 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);

Be sure you application has permission to internet and sdcard reading

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM