使用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();
}
我从这个 answer 。
当然,您需要拥有最新的Facebook SDK,但是我们需要应用这个补丁修复 {error:{type:OAuthException,message:(#352)不支持视频文件格式}} / code> string response error。
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屋!