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:
- Do no include "source" or "file_url" in the request parameters even though the FB documentation says so.
- 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
Getintent() Extras Always Null
Android: How to Open Another App from My App
How to Get Selected Items from Multi Select List View
Creating a PDF File in Android Programmatically and Writing in It
Android: How to Share Image with Text on Facebook via Intent
How to Crop the Parsed Image in Android
Getting a Photo from a Contact
How to Get Multiple Icons to Launch Different Activities in One Application
Custom Uri Schemes for the Facebook Messenger
Horizontalscrollview Inside Swiperefreshlayout
How to Implement a Contentobserver for Call Logs
Android Layout Folders: Layout, Layout-Port, Layout-Land
Extract Code Country from Phone Number [Libphonenumber]
Which Android Ide Is Better - Android Studio or Eclipse
How to Set the Language in Speech Recognition on Android
How to Change Bitmap Image Color in Android
How to Cancel an Dialog Themed Like Activity When Touched Outside the Window