Java – How do I upload videos to Facebook using the Facebook Android SDK 4.x?

How do I upload videos to Facebook using the Facebook Android SDK 4.x?… here is a solution to the problem.

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

I’m trying to change my Facebook SDK from 3.20 to 4.x. The new SDK cannot upload videos.

This is the code that runs 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 differences I’ve 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 would appreciate any help. For the new SDK, I haven’t found any video upload examples from Facebook yet.

Solution

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

There are also some warnings that feel more like bugs in Facebook’s Android SDK, but they are:

  1. Don’t include “source” or “file_url” in the request parameters, even if the FB documentation says so.
  2. Include video data for certain strings, such as video file names, in the request parameters.

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

Related Problems and Solutions