是否可以使用Facebook SDK将视频从SD卡上传到Facebook?

Eri*_*ick 13 android facebook facebook-graph-api

我们可以通过Facebook SDK将视频从Android设备的SD卡上传到Facebook帐户吗?

如果是这样,有什么简单的例子?

Eri*_*ick 25

对的,这是可能的!经过两天的尝试和研究,我能够做到.这是代码:

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();
}
Run Code Online (Sandbox Code Playgroud)

这里fbRequestListener()是一个实现AsyncFacebookRunner.RequestListener()readBytes()为您的视频文件转换的功能byte[].该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();
}
Run Code Online (Sandbox Code Playgroud)

我从这个答案得到了这个功能.

当然,您需要拥有最新的Facebook SDK,但我们需要应用此补丁来修复{"error":{"type":"OAuthException","message":"(#352) Video file format is not supported"}}字符串响应错误.

就是这样!我希望这有帮助!