Reputation: 654
06-21 12:35:37.275: E/AndroidRuntime(12402): FATAL EXCEPTION: Thread-1864
06-21 12:35:37.275: E/AndroidRuntime(12402): java.lang.OutOfMemoryError
06-21 12:35:37.275: E/AndroidRuntime(12402): at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)
06-21 12:35:37.275: E/AndroidRuntime(12402): at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201)
06-21 12:35:37.275: E/AndroidRuntime(12402): at libcore.net.http.RetryableOutputStream.write(RetryableOutputStream.java:61)
06-21 12:35:37.275: E/AndroidRuntime(12402): at java.io.BufferedOutputStream.write(BufferedOutputStream.java:131)
06-21 12:35:37.275: E/AndroidRuntime(12402): at java.io.OutputStream.write(OutputStream.java:82)
06-21 12:35:37.275: E/AndroidRuntime(12402): at com.facebook.android.Util.openUrl(Util.java:190)
06-21 12:35:37.275: E/AndroidRuntime(12402): at com.facebook.android.Facebook.request(Facebook.java:563)
06-21 12:35:37.275: E/AndroidRuntime(12402): at dev.env.secvideo.SaveActivity$FBRequestListener.onComplete(SaveActivity.java:467)
06-21 12:35:37.275: E/AndroidRuntime(12402): at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:254)
when i am trying to upload video to facebook from my app.i am getting this error message.my video size is around 15-20 MB. My code is running correctly in lower versions.I am getting this error only in higher version above 4.0
I am using following code
public byte[] readBytes(String path) throws IOException {
InputStream is = new FileInputStream(path);
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(path.length());
//int bufferSize = 30*1024;
byte[] buffer = new byte[path.length()];
int len = 0;
while ((len = is.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}
public class FBRequestListener implements RequestListener
{
@Override
public void onComplete(String response, Object state) {
byte[] data = null;
String path = Environment.getExternalStorageDirectory()+"/recordvideooutput.3gp";
String datapath = new File(path).getAbsolutePath();
String datamsg = "My Video";
Bundle params;
try {
//InputStream is = new FileInputStream(datapath);
//data = readBytes(is);
data = readBytes(datapath);
params = new Bundle();
params.putString(Facebook.TOKEN, access_token);
params.putString("message", datamsg);
params.putString("contentType", "video/quicktime");
params.putByteArray("video.mov", data);
String request = facebook.request("me/videos", params, "POST");
Log.i("request", request);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onIOException(IOException e, Object state) {
}
@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
}
@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
}
@Override
public void onFacebookError(FacebookError e, Object state) {
}
}
Upvotes: 3
Views: 1710
Reputation: 2259
Sometimes you need to increase the working memory available to eclipse but editting the eclipse.ini file found in the eclipse directory, the last line , the value default to 512m which means only 512mb has been availed as memory to eclipse, increase this and you will have more ram to your eclipse.
Upvotes: 0
Reputation: 8302
Basically, you cannot do it the way you are trying to do. Because you are reading the entire file into memory, you are exhausting the memory at your disposal before it ever gets to the network.
There are a few options you can do to help with this:
You can compress the video, meaning your 15MB can be reduced in size. Depending on the compression, you should be able to load this file into memory. However, this isn't a guarantee and you can still run into issues.
I have checked the Facebook API and it doesn't look like they support streaming data through their Android SDK. This means you need to roll your own. They do have an API to do it on their website (http://developers.facebook.com/docs/reference/api/video/) so you'd have to figure out how to work with that API and then, once you understand it, stream your data to their webservice instead of trying to load it all into a single facebook.request object.
There is a blog post here (http://developers.facebook.com/blog/post/493/) describing how to do it as a POST request. Specifically, quoting from the blog
To publish a video, issue a POST request with the video file attachment as multipart/form-data to https://graph-video.facebook.com/me/videos.
There is a PHP example that you might be able to take key pieces out of and convert to Java.
I would try to do the second approach if I were in your shoes. Hope that helps.
Upvotes: 0
Reputation: 414
Add android:largeHeap="true" under the application tag in your Android Manifest file. This increases the available memory space at your disposal. However, this only works on API 11+.
Upvotes: 2