Is Uploading Videos from an Sd Card to Facebook Possible with the Facebook Sdk

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

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!

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

Facebook for android video upload - works with one app and not with another

Solved.

The facebook app that works was created a few months ago.
The facebook app that doesn't work was created yesterday.

Apparently, in that interval facebook added a new kind of permission - "upload_video".
Even the app that worked never requested that permission, but it somehow uploaded the video regardless of it - maybe because of facebook's backwards compatibility.

The way I found this is by noticing that even the onComplete callback has a message, and in that message was the error.
Very stupid SDK behavior in my opinion - if I had an error, why call onComplete() and not onFacebookError()?

Upload Video to Facebook Problems

Follow this answer instead: Is uploading videos from an SD Card to Facebook possible with the Facebook SDK?

Basically you have to use AsyncFacebookRunner with the parameters message, filename, and data. Message is the short message to go with your video, filename is the type of file (ex: ".mp4"), and data is your video after converting into bytes.

Use AsyncFacebookRunner to POST this to "me/videos".

Reference: https://developers.facebook.com/docs/reference/rest/video.upload/



Related Topics



Leave a reply



Submit