Upload large video to Facebook through my app - out of memory!

  • Replies:4
Nhat Nguyen
  • Forum posts: 5

Jun 1, 2012, 4:31:04 PM via Website

Hi all,
I am develop a demo about Facebook SDK from my app. Now, my app can: post status, image , video to my wall or friend's wall.
But, when I am testing with large video size (about 12 MB), this get an OutOfMemory error and the app failed. In Logcat, the Facebook's line-code has problem, issue out of momory error.
My code, upload small video (~ 2MB) successful.
1byte data[] = readVideoData(videoFilePath);
2 if (data == null) return;
3 else Log.i(TAG, "-Video byte array length = " + data.length);
4
5 Bundle bundle = new Bundle();
6 bundle.putString(Facebook.TOKEN, mFacebook.getAccessToken());
7 bundle.putString("description", status);
8 bundle.putString("filename", new File(videoFilePath).getName());
9 bundle.putByteArray("video", data);
10
11 mFacebookRunner.request("me/videos", bundle, "POST",
12 new FBOperationRequestListener(), null);

From Facebook SDK for Android, error line is marked below (inside method openOrl):
1public static String openUrl(String url, String method, Bundle params)
2 throws MalformedURLException, IOException {
3......
4
5conn.setRequestMethod("POST");
6 conn.setRequestProperty(
7 "Content-Type",
8 "multipart/form-data;boundary="+strBoundary);
9 conn.setDoOutput(true);
10 conn.setDoInput(true);
11 conn.setRequestProperty("Connection", "Keep-Alive");
12 conn.connect();
13 os = new BufferedOutputStream(conn.getOutputStream());
14
15 os.write(("--" + strBoundary +endLine).getBytes());
16 os.write((encodePostBody(params, strBoundary)).getBytes());
17 os.write((endLine + "--" + strBoundary + endLine).getBytes());
18
19 if (!dataparams.isEmpty()) {
20
21 for (String key: dataparams.keySet()){
22// os.write(("Content-Disposition: form-data; filename=\"" + key + "\"" + endLine).getBytes());
23 os.write(("Content-Disposition: form-data; filename=\"" + (filename != null ? filename : key) + "\"" + endLine).getBytes());
24 os.write(("Content-Type: content/unknown" + endLine + endLine).getBytes());
25
26 (***) os.write(dataparams.getByteArray(key)); // Out Of Memory here
27
28 os.write((endLine + "--" + strBoundary + endLine).getBytes());
29
30 }
31 }
32 os.flush();
That line get data from Bundle we put before (array of byte).

Please help me with this problem, thanks a lot ^_^.

— modified on Jun 1, 2012, 4:32:03 PM

Reply
Jeremiah
  • Forum posts: 775

Jun 5, 2012, 5:31:34 PM via Website

Android apps have a heap size they are limited to. So your device may have a gigabyte or more of ram, but your app is limited to 24mb (smaller or bigger depending on the default heap size) that they are limited to. So what you need to do is instead of reading the video file and temporarily storing it in a byte array; you need to read small chunks of the video data and write them at the same time.

Reply
Eric McBride
  • Forum posts: 1,790

Jun 5, 2012, 5:49:30 PM via Website

Our coding God Jeremiah could probably help you with this one. You around Jeremiah?

Reply
Jeremiah
  • Forum posts: 775

Jun 5, 2012, 9:14:33 PM via Website

Eric McBride
Our coding God Jeremiah could probably help you with this one. You around Jeremiah?

hehe looks like i beat you to the post Eric! :lol:

Reply
Eric McBride
  • Forum posts: 1,790

Jun 6, 2012, 12:47:51 PM via Website

Jeremiah
Eric McBride
Our coding God Jeremiah could probably help you with this one. You around Jeremiah?

hehe looks like i beat you to the post Eric! :lol:

Holy crap you're fast! :grin: How the hell did that happen!!!

— modified on Jun 6, 2012, 12:48:05 PM

Reply