Upload image to FaceBook from SDCard

后端 未结 1 1561
遥遥无期
遥遥无期 2021-01-17 06:03

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

1条回答
  •  梦毁少年i
    2021-01-17 06:35

    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

    0 讨论(0)
提交回复
热议问题