如何使用Facebook Android SDK 4.x将视频上传到Facebook?

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

以下是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尝试的不同内容.但每次我得到同样的错误:

{FacebookServiceException:httpResponseCode:500,facebookErrorCode:6000,facebookErrorType:FacebookApiException,message:上传视频文件时出现问题.请使用其他文件重试.}

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

我很感激任何帮助.对于新的SDK,我还没有从Facebook上找到任何视频上传样本.

最佳答案
花了1.5天后,我终于有了工作.基本思想是将视频作为multipart / form-data发送,在这种情况下我使用的是byteArray.我从Bhavesh Hirpara给出的关于这个问题的答案中得到了这个想法:
Is uploading videos from an SD Card to Facebook possible with the Facebook SDK?

还有几个警告,感觉更像Facebook Android SDK中的错误,但它们是:

>请勿在请求参数中包含“source”或“file_url”,即使FB文档是这样说的.
>在请求参数中包含针对某些字符串(例如视频文件名)的视频数据.

这是工作代码.

    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? - 代码日志