Upload Video to Facebook in Android

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

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?

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

  1. Do no include "source" or "file_url" in the request parameters even though the FB documentation says so.
  2. Include the video data against some String (e.g. video file name) in the request parameters.

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

Posting video to facebook in android

I am using Facebook sdk 3.14 and the code below is working just fine

Bundle postParams = new Bundle();
postParams.putString("name", give a title);
postParams.putString("type", "link");
postParams.putString("link", enclosing url you want to share);
Request request = new Request(session,"me/feed" , postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();

and this will be the callback

Request.Callback callback = new Request.Callback() {
@Override
public void onCompleted(Response response) {
FacebookRequestError error = response.getError();
if(error!=null){
something went wrong
}else{
successfully posted
}
}

};

Try this and give a feedback.

Upload videos to Facebook from android application

Hello everyone I have search on net and I found some help for my question. I have successfully implemented video uploading but some steps you need to take before you upload.

Please refer this link and implement the given case. It will allows you to do login and then use this link to implement video upload.

I have solved my problem but I have posted this answer just to help those people who face the similar problem.



Related Topics



Leave a reply



Submit