android - 使用 Facebook SDK 可以将视频从 SD 卡上传到 Facebook 吗?

标签 android facebook facebook-graph-api

我们可以通过 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/

相关文章:

javascript - 单击输入字段触发窗口调整大小

android - phonegap android 应用程序因打开相机时内存不足而崩溃

Facebook JavaScript SDK : Login button not showing up

ios - 如何在同一相册的 Facebook 和 Twitter 上使用 SLComposeViewController 发布多张图片

java - 我将通过使用友元操作检索friendMemberList,但我总是有列表的最后一个成员?

java - android通知适用于循环中的一个对象

java - 一种在 TextView 中更改文本时间的方法?

javascript - Facebook 如何管理用户 session ?

node.js - FacebookGraphAPIError : (#100) Tried accessing nonexisting field (user_photos) on node type (User)

php - 通过 Facebook 的图形 API 发布状态