我需要将图像从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
我在做什么错? 感谢
答案 0 :(得分:0)
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;
}