如何使用Facebook Android SDK 4.x将视频上传到Facebook? [英] How to upload videos to Facebook using Facebook Android SDK 4.x?

查看:229
本文介绍了如何使用Facebook Android SDK 4.x将视频上传到Facebook?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的Facebook SDK从3.20更改为4.x。视频上传被新的SDK破坏。

I am trying to change my Facebook SDK from 3.20 to 4.x. The video upload gets broken with the new SDK.

以下是3.20中的代码:

Here is the code that is working in 3.20:

    Request request = Request.newUploadVideoRequest(session, new File(videoPath), callback);
    Bundle params = request.getParameters();
    params.putString("title", albumName);
    params.putString("description", " #SomeTag");
    request.setParameters(params);
    request.executeAsync();

以下是我用新的SDK 4.x尝试的不同之处。但是每次遇到相同的错误:

Here are the different things I have tried with the new SDK 4.x. But every time I get the same error:

{FacebookServiceException:httpResponseCode:500,facebookErrorCode:6000,facebookErrorType:FacebookApiException,message:上传您的视频文件时出现问题。请再试一次另一个文件。}

{FacebookServiceException: httpResponseCode: 500, facebookErrorCode: 6000, facebookErrorType: FacebookApiException, message: There was a problem uploading your video file. Please try again with another file.}

1。

    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    GraphRequest request = GraphRequest.newPostRequest(accessToken, "me/videos", null, callback);
    Bundle params = request.getParameters();
    params.putString("file_url", videoPath);
    params.putString("title", albumName);
    File videoFile = new File(videoPath);
    ParcelFileDescriptor descriptor = ParcelFileDescriptor.open(videoFile, ParcelFileDescriptor.MODE_READ_ONLY);
    params.putParcelable("source", descriptor);
    params.putString("description", " #SomeTag");
    request.setParameters(params);
    request.executeAsync();

2。

    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    GraphRequest request = GraphRequest.newPostRequest(accessToken, "me/videos", null, callback);
    Bundle params = request.getParameters();
    params.putString("file_url", videoPath);
    params.putString("title", albumName);
    byte[] byteVideo = getFileByteArray(videoPath);
    params.putByteArray("source", byteVideo);
    params.putString("description", " #SomeTag");
    request.setParameters(params);
    request.executeAsync();

3。

    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    GraphRequest request = GraphRequest.newPostRequest(accessToken, "me/videos", null, callback);
    Bundle params = request.getParameters();
    params.putString("file_url", videoPath);
    params.putString("title", albumName);
    params.putString("source", "{video-data}");
    params.putString("description", " #SomeTag");
    request.setParameters(params);
    request.executeAsync();

感谢任何帮助。我没有发现任何视频上传样本来自Facebook或者为新的SDK。

I'd appreciate any help. I have not found any video upload sample from Facebook either for the new SDK.

推荐答案

在花了1.5天后,我终于有了加工。基本思想是将视频作为multipart / form-data发送,在这种情况下我正在使用一个byteArray。我从这个问题的 Bhavesh Hirpara 提供的答案中得到了这个想法:
将视频从SD卡上传到Facebook有可能使用Facebook SDK?

After spending 1.5 days, I finally have it working. The basic idea is to send the video as multipart/form-data, in this case I am using a byteArray. I got this idea from the answer given by Bhavesh Hirpara on this question : Is uploading videos from an SD Card to Facebook possible with the Facebook SDK?

有更多的注意事项,更多的是像Facebook Android SDK中的错误,但它们是:

There are couple of more caveats, which feel more like bugs in Facebook Android SDK, but they are:


  1. 即使FB文档说明,也不要在请求参数中包含source或file_url。

  2. 在请求参数中包含一些String(例如视频文件名)的视频数据。

这是工作代码。 p>

Here is the working code.

    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    GraphRequest request = GraphRequest.newPostRequest(accessToken, "me/videos", null, callback);
    Bundle params = request.getParameters();
    try {
        byte[] data = readBytes(videoPath);
        params.putByteArray("video.mp4", data);
        params.putString("title", albumName);
        params.putString("description", " #SomeTag");
        request.setParameters(params);
        request.executeAsync();
    }
    catch (Exception e) {
        e.printStackTrace();
    }


    public byte[] readBytes(String dataPath) throws IOException {

        InputStream inputStream = new FileInputStream(dataPath);
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];

        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }

        return byteBuffer.toByteArray();
    }

这篇关于如何使用Facebook Android SDK 4.x将视频上传到Facebook?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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