我正在尝试将视频从我的厨房上传到 Facebook 墙上这是我的代码
byte[] data = null;
String dataPath = "/mnt/sdcard/DCIM/Camera/video-2012-09-03-13-34-19.mp4";
Bundle param;
//video extension
data Name = ".mp4"
facebook = new Facebook(FB_APP_ID);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
InputStream is = null;
try {
is = new FileInputStream(dataPath);
data = readBytes(is);
param = new Bundle();
param.putString("message", "hello guys");
param.putString("filename", data Name);
param.putByteArray("video", data);
mAsyncRunner.request("me/videos", param, "POST", new fbRequestListener(), null);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
.....................
public byte[] readBytes(InputStream inputStream) throws IOException {
// This dynamically extends to take the bytes you read.
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
// This is storage overwritten on each iteration with bytes.
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
// We need to know how may bytes were read to write them to the byteBuffer.
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
// And then we can return your byte array.
return byteBuffer.toByteArray();
}
首先我在日志猫中收到这样的警告
W/Bundle ( 774): Attempt to cast generated internal exception:
W/Bundle ( 774): java.lang.ClassCastException: java.lang.String
W/Bundle ( 774): at android.os.Bundle.getByteArray(Bundle.java:1305)
W/Bundle ( 774): at com.facebook.android.Util.openUrl(Util.java:172)
W/Bundle ( 774): at com.facebook.android.Facebook.request(Facebook.java:751)
W/Bundle ( 774): at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:253)
W/Bundle ( 774): Key format expected byte[] but value was a java.lang.String. The default value <null> was returned.
W/Bundle ( 774): Attempt to cast generated internal exception:
W/Bundle ( 774): java.lang.ClassCastException: java.lang.String
W/Bundle ( 774): at android.os.Bundle.getByteArray(Bundle.java:1305)
W/Bundle ( 774): at com.facebook.android.Util.openUrl(Util.java:172)
W/Bundle ( 774): at com.facebook.android.Facebook.request(Facebook.java:751)
我在 Google 中搜索,有人建议 Facebook API 中存在错误,因为他们建议我修改 Util.java 中的一些行
for (String key : parameters.keySet()) {
if (parameters.get(key) instanceof byte[]) {
continue;
}
sb.append("Content-Disposition: form-data; name=\"" + key
+ "\"\r\n\r\n" + parameters.getString(key));
sb.append("\r\n" + "--" + boundary + "\r\n");
}
return sb.toString();
}
和
for (String key : params.keySet()) {
if (params.get(key) instanceof byte[]) {
dataparams.putByteArray(key, params.getByteArray(key));
}
}
现在我没有收到任何警告任何错误,但我的视频没有上传请帮助我,我在这里感到震惊。
提前致谢