是否可以使用 Facebook SDK 将视频从 SD 卡上传到 Facebook? [英] Is uploading videos from an SD Card to Facebook possible with the Facebook SDK?
问题描述
我们可以通过 Facebook SDK 将视频从 Android 设备的 SD 卡上传到 Facebook 帐户吗?
Can we upload videos from an Android device's SD Card to a Facebook account via the Facebook SDK?
如果有,有哪些简单的例子?
If so, what are some simple examples?
推荐答案
是的,这是可能的!经过两天的尝试和研究,我能够做到.代码如下:
Yes, it is possible! After two days of trying and researching, I was able to do it. Here's the code:
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 等).代码如下:
where fbRequestListener()
is an implementation of AsyncFacebookRunner.RequestListener()
and readBytes()
is a function of converting your video file to byte[]
. The dataName
string should include a valid file extension (3gp, mp4, etc.). The code is as follows:
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();
}
我从这个回答.
当然,你需要有最新的Facebook SDK,但是我们需要应用这个patch 修复{"error":{"type":"OAuthException","message":"(#352) 不支持视频文件格式"}}
字符串响应错误.
Of course, you need to have the latest Facebook SDK, but we need to apply this patch to fix the {"error":{"type":"OAuthException","message":"(#352) Video file format is not supported"}}
string response error.
就是这样!我希望这会有所帮助!
And that's it! I hope this helps!
这篇关于是否可以使用 Facebook SDK 将视频从 SD 卡上传到 Facebook?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!