Android 安卓facebook从SD卡上传图片

Android 安卓facebook从SD卡上传图片,android,facebook,image,post,Android,Facebook,Image,Post,我需要把SD卡上的图片贴到我的fb墙上 为此我做了这件事 private void postToFacebook(String review) { mProgress.setMessage("Posting ..."); mProgress.show(); AsyncFacebookRunner mAsyncFbRunner = new AsyncFacebookRunner(mFacebook); Bundle par

我需要把SD卡上的图片贴到我的fb墙上 为此我做了这件事

private void postToFacebook(String review) {    
        mProgress.setMessage("Posting ...");
        mProgress.show();

        AsyncFacebookRunner mAsyncFbRunner = new AsyncFacebookRunner(mFacebook);

        Bundle params = new Bundle();

//      params.putString("message", review);
        params.putString("message", GlobalVariable.getMsg());
        params.putString("name", "Dexter");
        params.putString("caption", "londatiga.net");
        params.putString("link", "http://www.londatiga.net");
        params.putString("description", "Dexter, seven years old dachshund who loves to catch cats, eat carrot and krupuk");
    //  params.putString("picture", "http://twitpic.com/show/thumb/6hqd44"); this runns fine and post img
        //params.putString("picture","file:///mnt/sdcard/IMAGE.PNG");this do nothing niether post string nor img
        mAsyncFbRunner.request("me/feed", params, "POST", new WallPostListener());
编辑

 Bitmap bi = BitmapFactory.decodeFile("/sdcard/test.jpg");
           ByteArrayOutputStream baos = new ByteArrayOutputStream();
           bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
           data = baos.toByteArray();


        //     Log.d("onCreate", "debug error  e = " + e.toString());

        Bundle params = new Bundle();
          params.putByteArray("picture", data);
Logcat

02-24 14:12:11.501: I/System.out(16979): Not a DRM File, opening notmally
02-24 14:12:11.501: I/System.out(16979): buffer returned 
02-24 14:12:11.511: I/System.out(16979): Not a DRM File, opening notmally
02-24 14:12:11.511: I/System.out(16979): buffer returned 
02-24 14:12:11.792: D/Facebook-Util(16979): POST URL: https://graph.facebook.com/me/feed
02-24 14:12:11.792: W/Bundle(16979): Key message expected byte[] but value was a java.lang.String.  The default value <null> was returned.
02-24 14:12:11.802: I/Adreno200-EGLSUB(16979): <ConfigWindowMatch:2087>: Format RGBA_8888.
02-24 14:12:11.812: W/Bundle(16979): Attempt to cast generated internal exception:
02-24 14:12:11.812: W/Bundle(16979): java.lang.ClassCastException: java.lang.String cannot be cast to byte[]
02-24 14:12:11.812: W/Bundle(16979):    at android.os.Bundle.getByteArray(Bundle.java:1370)
02-24 14:12:11.812: W/Bundle(16979):    at com.facebook.android.Util.openUrl(Util.java:155)
02-24 14:12:11.812: W/Bundle(16979):    at com.facebook.android.Facebook.request(Facebook.java:559)
02-24 14:12:11.812: W/Bundle(16979):    at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:208)
02-24 14:12:11.832: W/Bundle(16979): Key access_token expected byte[] but value was a java.lang.String.  The default value <null> was returned.
02-24 14:12:11.832: W/Bundle(16979): Attempt to cast generated internal exception:
02-24 14:12:11.832: W/Bundle(16979): java.lang.ClassCastException: java.lang.String cannot be cast to byte[]
02-24 14:12:11.832: W/Bundle(16979):    at android.os.Bundle.getByteArray(Bundle.java:1370)
02-24 14:12:11.832: W/Bundle(16979):    at com.facebook.android.Util.openUrl(Util.java:155)
02-24 14:12:11.832: W/Bundle(16979):    at com.facebook.android.Facebook.request(Facebook.java:559)
02-24 14:12:11.832: W/Bundle(16979):    at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:208)
02-24 14:12:11.832: W/Bundle(16979): Key format expected byte[] but value was a java.lang.String.  The default value <null> was returned.
02-24 14:12:11.842: W/Bundle(16979): Attempt to cast generated internal exception:
02-24 14:12:11.842: W/Bundle(16979): java.lang.ClassCastException: java.lang.String cannot be cast to byte[]
02-24 14:12:11.842: W/Bundle(16979):    at android.os.Bundle.getByteArray(Bundle.java:1370)
02-24 14:12:11.842: W/Bundle(16979):    at com.facebook.android.Util.openUrl(Util.java:155)
02-24 14:12:11.842: W/Bundle(16979):    at com.facebook.android.Facebook.request(Facebook.java:559)
02-24 14:12:11.842: W/Bundle(16979):    at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:208)
02-24 14:12:11.872: D/SensorManager(16979): unregisterListener::  Listener= android.view.OrientationEventListener$SensorEventListenerImpl@428008b8
02-24 14:12:11.872: D/Sensors(16979): Remain listener = Sending .. normal delay 200ms
02-24 14:12:11.872: I/Sensors(16979): sendDelay --- 200000000
02-24 14:12:11.882: D/SensorManager(16979): JNI - sendDelay
02-24 14:12:11.882: I/SensorManager(16979): Set normal delay = true
02-24 14:12:14.805: W/Bundle(16979): Key message expected byte[] but value was a java.lang.String.  The default value <null> was returned.
02-24 14:12:14.805: W/Bundle(16979): Attempt to cast generated internal exception:
02-24 14:12:14.805: W/Bundle(16979): java.lang.ClassCastException: java.lang.String cannot be cast to byte[]
02-24 14:12:14.805: W/Bundle(16979):    at android.os.Bundle.getByteArray(Bundle.java:1370)
02-24 14:12:14.805: W/Bundle(16979):    at com.facebook.android.Util.encodePostBody(Util.java:63)
02-24 14:12:14.805: W/Bundle(16979):    at com.facebook.android.Util.openUrl(Util.java:182)
02-24 14:12:14.805: W/Bundle(16979):    at com.facebook.android.Facebook.request(Facebook.java:559)
02-24 14:12:14.805: W/Bundle(16979):    at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:208)
02-24 14:12:14.805: W/Bundle(16979): Key method expected byte[] but value was a java.lang.String.  The default value <null> was returned.
02-24 14:12:14.805: W/Bundle(16979): Attempt to cast generated internal exception:
02-24 14:12:14.805: W/Bundle(16979): java.lang.ClassCastException: java.lang.String cannot be cast to byte[]
02-24 14:12:14.805: W/Bundle(16979):    at android.os.Bundle.getByteArray(Bundle.java:1370)
02-24 14:12:14.805: W/Bundle(16979):    at com.facebook.android.Util.encodePostBody(Util.java:63)
02-24 14:12:14.805: W/Bundle(16979):    at com.facebook.android.Util.openUrl(Util.java:182)
02-24 14:12:14.805: W/Bundle(16979):    at com.facebook.android.Facebook.request(Facebook.java:559)
02-24 14:12:14.805: W/Bundle(16979):    at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:208)
02-24 14:12:14.815: W/Bundle(16979): Key format expected byte[] but value was a java.lang.String.  The default value <null> was returned.
02-24 14:12:14.815: W/Bundle(16979): Attempt to cast generated internal exception:
02-24 14:12:14.815: W/Bundle(16979): java.lang.ClassCastException: java.lang.String cannot be cast to byte[]
02-24 14:12:14.815: W/Bundle(16979):    at android.os.Bundle.getByteArray(Bundle.java:1370)
02-24 14:12:14.815: W/Bundle(16979):    at com.facebook.android.Util.encodePostBody(Util.java:63)
02-24 14:12:14.815: W/Bundle(16979):    at com.facebook.android.Util.openUrl(Util.java:182)
02-24 14:12:14.815: W/Bundle(16979):    at com.facebook.android.Facebook.request(Facebook.java:559)
02-24 14:12:14.815: W/Bundle(16979):    at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:208)
02-24 14:12:14.815: W/Bundle(16979): Key access_token expected byte[] but value was a java.lang.String.  The default value <null> was returned.
02-24 14:12:14.815: W/Bundle(16979): Attempt to cast generated internal exception:
02-24 14:12:14.815: W/Bundle(16979): java.lang.ClassCastException: java.lang.String cannot be cast to byte[]
02-24 14:12:14.815: W/Bundle(16979):    at android.os.Bundle.getByteArray(Bundle.java:1370)
02-24 14:12:14.815: W/Bundle(16979):    at com.facebook.android.Util.encodePostBody(Util.java:63)
02-24 14:12:14.815: W/Bundle(16979):    at com.facebook.android.Util.openUrl(Util.java:182)
02-24 14:12:14.815: W/Bundle(16979):    at com.facebook.android.Facebook.request(Facebook.java:559)
02-24 14:12:14.815: W/Bundle(16979):    at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:208)
02-24 14:12:22.743: D/SensorManager(16979): registerListener :: handle = 4  name= MPL accel delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@428008b8
02-24 14:12:40.312: D/SensorManager(16979): unregisterListener::  Listener= android.view.OrientationEventListener$SensorEventListenerImpl@428008b8
02-24 14:12:40.312: D/Sensors(16979): Remain listener = Sending .. normal delay 200ms
02-24 14:12:40.312: I/Sensors(16979): sendDelay --- 200000000
02-24 14:12:40.312: D/SensorManager(16979): JNI - sendDelay
02-24 14:12:40.312: I/SensorManager(16979): Set normal delay = true
02-24 14:12:40.402: W/IInputConnectionWrapper(16979): getSelectedText on inactive InputConnection
02-24 14:12:40.402: W/IInputConnectionWrapper(16979): setComposingText on inactive InputConnection
02-24 14:12:11.501:I/System.out(16979):不是DRM文件,正在打开
02-24 14:12:11.501:I/System.out(16979):返回缓冲区
02-24 14:12:11.511:I/System.out(16979):不是DRM文件,正在打开
02-24 14:12:11.511:I/System.out(16979):返回缓冲区
02-24 14:12:11.792:D/Facebook-Util(16979):发布URL:https://graph.facebook.com/me/feed
02-24 14:12:11.792:W/Bundle(16979):关键消息应为字节[],但值为java.lang.String。返回了默认值。
02-24 14:12:11.802:I/Adreno200 EGLSUB(16979)::格式RGBA_8888。
02-24 14:12:11.812:W/捆绑包(16979):尝试强制转换生成的内部异常:
02-24 14:12:11.812:W/Bundle(16979):java.lang.ClassCastException:java.lang.String不能转换为字节[]
02-24 14:12:11.812:W/Bundle(16979):在android.os.Bundle.getByteArray(Bundle.java:1370)
02-24 14:12:11.812:W/Bundle(16979):位于com.facebook.android.Util.openUrl(Util.java:155)
02-24 14:12:11.812:W/Bundle(16979):位于com.facebook.android.facebook.request(facebook.java:559)
02-24 14:12:11.812:W/Bundle(16979):位于com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:208)
02-24 14:12:11.832:W/Bundle(16979):密钥访问令牌应为字节[],但值为java.lang.String。返回了默认值。
02-24 14:12:11.832:W/捆绑包(16979):尝试强制转换生成的内部异常:
02-24 14:12:11.832:W/Bundle(16979):java.lang.ClassCastException:java.lang.String不能转换为字节[]
02-24 14:12:11.832:W/Bundle(16979):在android.os.Bundle.getByteArray(Bundle.java:1370)
02-24 14:12:11.832:W/Bundle(16979):位于com.facebook.android.Util.openUrl(Util.java:155)
02-24 14:12:11.832:W/Bundle(16979):位于com.facebook.android.facebook.request(facebook.java:559)
02-24 14:12:11.832:W/Bundle(16979):位于com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:208)
02-24 14:12:11.832:W/Bundle(16979):键格式应为byte[],但值为java.lang.String。返回了默认值。
02-24 14:12:11.842:W/捆绑包(16979):尝试强制转换生成的内部异常:
02-24 14:12:11.842:W/Bundle(16979):java.lang.ClassCastException:java.lang.String不能转换为字节[]
02-24 14:12:11.842:W/Bundle(16979):在android.os.Bundle.getByteArray(Bundle.java:1370)
02-24 14:12:11.842:W/Bundle(16979):位于com.facebook.android.Util.openUrl(Util.java:155)
02-24 14:12:11.842:W/Bundle(16979):位于com.facebook.android.facebook.request(facebook.java:559)
02-24 14:12:11.842:W/Bundle(16979):位于com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:208)
02-24 14:12:11.872:D/SensorManager(16979):取消注册Listener::Listener=android.view.OrientationEventListener$SensorEventListenerImpl@428008b8
02-24 14:12:11.872:D/传感器(16979):保持侦听器=发送。。正常延迟200ms
02-24 14:12:11.872:I/传感器(16979):发送延迟--200000000
02-24 14:12:11.882:D/SensorManager(16979):JNI-发送延迟
02-24 14:12:11.882:I/SensorManager(16979):设置正常延迟=真
02-24 14:12:14.805:W/Bundle(16979):关键消息应为字节[],但值为java.lang.String。返回了默认值。
02-24 14:12:14.805:W/捆绑包(16979):尝试强制转换生成的内部异常:
02-24 14:12:14.805:W/Bundle(16979):java.lang.ClassCastException:java.lang.String不能转换为字节[]
02-24 14:12:14.805:W/Bundle(16979):在android.os.Bundle.getByteArray(Bundle.java:1370)
02-24 14:12:14.805:W/Bundle(16979):位于com.facebook.android.Util.encodePostBody(Util.java:63)
02-24 14:12:14.805:W/Bundle(16979):位于com.facebook.android.Util.openUrl(Util.java:182)
02-24 14:12:14.805:W/Bundle(16979):位于com.facebook.android.facebook.request(facebook.java:559)
02-24 14:12:14.805:W/Bundle(16979):位于com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:208)
02-24 14:12:14.805:W/Bundle(16979):键方法应为byte[],但值为java.lang.String。返回了默认值。
02-24 14:12:14.805:W/捆绑包(16979):尝试强制转换生成的内部异常:
02-24 14:12:14.805:W/Bundle(16979):java.lang.ClassCastException:java.lang.String不能转换为字节[]
02-24 14:12:14.805:W/Bundle(16979):在android.os.Bundle.getByteArray(Bundle.java:1370)
02-24 14:12:14.805:W/Bundle(16979):位于com.facebook.android.Util.encodePostBody(Util.java:63)
02-24 14:12:14.805:W/Bundle(16979):位于com.facebook.android.Util.openUrl(Util.java:182)
02-24 14:12:14.805:W/Bundle(16979):位于com.facebook.android.facebook.request(facebook.java:559)
02-24 14:12:14.805:W/Bundle(16979):位于com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:208)
02-24 14:12:14.815:W/Bundle(16979):键格式应为byte[],但值为java.lang.String。返回了默认值。
02-24 14:12:14.815:W/捆绑包(16979):尝试强制转换生成的内部异常:
02-24 14:12:14.815:W/Bundle(16979):java.lang.ClassCastException:java.lang.String不能转换为字节[]
02-24 14:12:14.815:W/Bundle(16979):在android.os.Bundle.getByteArray(Bundle.java:1370)
02-24 14:12:14.815:W/Bundle(16979):位于com.facebook.android.Util.encodePostBody(Util.java:63)
02-24 14:12:14.815:W/Bundle(16979):位于com.facebook.android.Util.openUrl(Util.java:182)
02-24 14:12:14.815:W/Bundle(16979):位于com.facebook.android.facebook.request(facebook.java:559)
02-24 14:12:14.815:W/Bundle(16979):位于com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:208)
02-24 14:12:14.815:W/Bundle(16979):密钥访问令牌应为字节[],但值为java.lang.String。返回了默认值。
02-24 14:12:14.815:W/捆绑包(16979):尝试强制转换生成的内部异常:
02-24 14:12:14.815:W/Bundle(16979):java.lang.ClassCastException:java.lang.String无法转换为byt
public void postPhoto() {


    mProgress = new ProgressDialog(context);
    mProgress.setMessage("Posting To Facebook...");
    mProgress.show();
    // Posting image to Facebook
    AsyncFacebookRunner mAsyncFbRunner = new AsyncFacebookRunner(facebook);


    try {

        Bitmap bi = decodeScaledBitmapFromSdCard("filepath", 600,
        480)
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bi.compress(Bitmap.CompressFormat.JPEG, 80, baos);
        data = baos.toByteArray();

        Bundle params = new Bundle();
        params.putString("method", "photos.upload");
        params.putString("name", "Test");
        params.putString("caption", "Test");
        params.putString("description", "Test");
        params.putByteArray("picture", data);
        mAsyncFbRunner.request(null, params, "POST",
                new WallPostListener(), null);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public Bitmap decodeScaledBitmapFromSdCard(String filePath, int reqWidth,
        int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filePath, options);
}

public int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and
        // width
        final int heightRatio = Math.round((float) height
                / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will
        // guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}