How to upload videos to Facebook using Facebook Android SDK 4.x?

时间:2015-05-24 21:37:46

标签: java android facebook facebook-android-sdk

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

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

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

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

2 个答案:

答案 0 :(得分:4)

花了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中的错误,但它们是:

  1. 不包括"来源"或" file_url"在请求参数中,即使FB文档说明了这一点。
  2. 在请求参数中包含针对某些字符串(例如视频文件名)的视频数据。
  3. 这是工作代码。

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

答案 1 :(得分:0)

试试这个。 Uri videoFileUri = ... ShareVideo = new ShareVideo.Builder()         .setLocalUrl(videoUrl)         。建立(); ShareVideoContent content = new ShareVideoContent.Builder()         .setVideo(ShareVideo)         .build();