在上传的视频从SD卡给Facebook可能与Facebook的SDK? [英] Is uploading videos from an SD Card to Facebook possible with the Facebook SDK?

查看:178
本文介绍了在上传的视频从SD卡给Facebook可能与Facebook的SDK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以从通过Facebook的SDK的Andr​​oid设备的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?

推荐答案

是的,这是可能的!经过尝试和研究了两天,我能做到这一点。 这里的code:

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的实现()为您的视频文件转换为字节[] 的功能。该数据名字符串应该包含一个有效的文件扩展名(3GP,MP4等)。在code是如下:

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();
}

我是从这个这个功能<一href="http://stackoverflow.com/questions/2436385/android-getting-from-a-uri-to-an-inputstream-to-a-byte-array">answer.

当然,你需要有最新的Facebook SDK,但我们需要应用此<一个href="https://github.com/johnmph/facebook-android-sdk/commit/d0e6ca9d3ecfbe964e72803fdd340cfeafa6ee2e"相对=nofollow>补丁修复 {错误:不支持(#352)视频文件格式:{类型:OAuthException,消息 }} 字符串响应错误。

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!

这篇关于在上传的视频从SD卡给Facebook可能与Facebook的SDK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆