我们可以通过 Facebook SDK 将视频从 Android 设备的 SD 卡上传到 Facebook 帐户吗?
如果是这样,有哪些简单的例子?
最佳答案
是的,这是可能的!经过两天的尝试和研究,我能够做到。 这是代码:
byte[] data = null;
String dataPath = "/mnt/sdcard/KaraokeVideos/myvideo.3gp";
String dataMsg = "Your video description here.";
Bundle param;
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", dataMsg);
param.putString("filename", dataName);
param.putByteArray("video", data);
mAsyncRunner.request("me/videos", param, "POST", new fbRequestListener(), null);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
其中 fbRequestListener()
是 AsyncFacebookRunner.RequestListener()
的实现,readBytes()
是将视频文件转换为字节[]
。 dataName
字符串应包含有效的文件扩展名(3gp、mp4 等)。代码如下:
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();
}
我从这个 answer 得到这个函数.
当然,你需要有最新的Facebook SDK,但是我们需要应用这个patch修复 {"error":{"type":"OAuthException","message":"(#352) Video file format is not supported"}}
字符串响应错误。
就是这样!希望对您有所帮助!
关于android - 使用 Facebook SDK 可以将视频从 SD 卡上传到 Facebook 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6908413/