In my app i am giving video file path to FileInputStream class after that i want it to convert into byte Array but it throws Out of memory Exception Please suggest me how to do it.Iam posting the code here thanks in advance.
public byte[] readBytes(InputStream inputStream) throws IOException
{
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1)
{
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}
-
1past your error log and code here..RajaReddy PolamReddy– RajaReddy PolamReddy2011年12月07日 09:17:59 +00:00Commented Dec 7, 2011 at 9:17
-
instead downloading whole video file why dont you use live streaming!! also I havent used DownloadManager so I dont know if downloadmanager handles this type of downloading or not but you can try it.vikky– vikky2011年12月07日 10:07:45 +00:00Commented Dec 7, 2011 at 10:07
-
Thanks for quick reply the file was not so big it is just 7 mb. logcat showing the fowlling error Out of memory: Heap Size=13575KB, Allocated=6793KB, Bitmap Size=14KB, Limit=20480KB 12-07 15:39:05.207: ERROR/dalvikvm(5132): Extra info: Footprint=13575KB, Allowed Footprint=13575KB, Trimmed=816KB 12-07 15:39:05.217: ERROR/AndroidRuntime(5132): Thread-10 1 java.lang.OutOfMemoryError: (Heap Size=13575KB, Allocated=6793KB, Bitmap Size=14KB) 12-07 15:39:05.217: ERROR/AndroidRuntime(5132):at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)raju– raju2011年12月07日 10:20:32 +00:00Commented Dec 7, 2011 at 10:20
-
Actually my aim is to upload a video from sdcard to facebook for that i am converting to byte array.raju– raju2011年12月07日 10:26:13 +00:00Commented Dec 7, 2011 at 10:26
-
@Rekha please give a try to my answerCaner– Caner2011年12月07日 11:09:30 +00:00Commented Dec 7, 2011 at 11:09
2 Answers 2
It fails because on the device you are testing you are allowed to use about 20MB of heap. At the time it fails, your app is already using 13.5MB of memory. When you are trying to convert ByteArrayOutputStream to byte array it is trying to allocate a new ~7MB size byte[] which fails because there is not enough memory. One workaround is to avoid this new allocation by reusing internal byte[] of ByteArrayOutputStream. Unfortunately there is no way to access it externally, so you need to create your own way like this:
First create MyByteArrayOutputStream.java:
public MyByteArrayOutputStream extends ByteArrayOutputStream {
@Override
public synchronized byte[] toByteArray() {
return buf;
}
}
And in your code:
public byte[] readBytes(InputStream inputStream) throws IOException
{
MyByteArrayOutputStream byteBuffer = new MyByteArrayOutputStream(); // note the change!
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1)
{
byteBuffer.write(buffer, 0, len);
}
inputStream.close(); // hopefully this will release some more memory
return byteBuffer.toByteArray();
}
Note that, this will still fail with large videos. Optimally you should try to upload big files in smaller chunks.
Comments
Seems straight forward, the video file is too large to fit in memory. Are you 100% certain you need to place the whole file in memory? This is usually a bad design concept.