Is uploading videos from an SD Card to Facebook possible with the Facebook SDK?

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?


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

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

I got this function from this answer.

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!


With the release of new facebook SDK 3.5, video uploading has been made easier. If you are using sdk 3.5 or above here is the code for uploading video to facebook

//Path to the video, Ex: path = Environment.getExternalStorageDirectory() + File.separator + "myVideo.mp4";
        String path;
        //get the current active facebook session
        Session session = Session.getActiveSession();
        //If the session is open
        if(session.isOpened()) {
            //Get the list of permissions associated with the session
            List<String> permissions = session.getPermissions();
            //if the session does not have video_upload permission
            if(!permissions.contains("video_upload")) {
                //Get the permission from user to upload the video to facebook
                Session.NewPermissionsRequest newPermissionsRequest = new Session
                        .NewPermissionsRequest(this, Arrays.asList("video_upload"));
                session.requestNewReadPermissions(newPermissionsRequest);
            }


            //Create a new file for the video 
            File file = new File(path);
            try {
                //create a new request to upload video to the facebook
                Request videoRequest = Request.newUploadVideoRequest(session, file, new Request.Callback() {

                    @Override
                    public void onCompleted(Response response) {

                        if(response.getError()==null)
                        {
                            Toast.makeText(MainActivity.this, "video shared successfully", Toast.LENGTH_SHORT).show();
                        }
                        else
                        {
                            Toast.makeText(MainActivity.this, response.getError().getErrorMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                //Execute the request in a separate thread
                videoRequest.executeAsync();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        //Session is not open
        else {
            Toast.makeText(getApplicationContext(), "Please login to facebook first", Toast.LENGTH_SHORT).show();
        }