0

I am trying to read video files in a byte[] and then encrypt them. The files which are relatively large i.e. more than 30 mb are giving out of memory exception. Here is the code I am using to read the file to byte[]:

FileInputStream fin=new FileInputStream(file);
byte[] fileContent = new byte[(int)file.length()];
fin.read(fileContent);

This is the error I am getting:

05-17 05:54:59.356: E/AndroidRuntime(4082): FATAL EXCEPTION: main
05-17 05:54:59.356: E/AndroidRuntime(4082): java.lang.OutOfMemoryError
05-17 05:54:59.356: E/AndroidRuntime(4082):     at com.vencrypto.ExplorerActivity$3.onItemClick(ExplorerActivity.java:136)
05-17 05:54:59.356: E/AndroidRuntime(4082):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
05-17 05:54:59.356: E/AndroidRuntime(4082):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
05-17 05:54:59.356: E/AndroidRuntime(4082):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
05-17 05:54:59.356: E/AndroidRuntime(4082):     at android.widget.AbsListView$1.run(AbsListView.java:3463)
05-17 05:54:59.356: E/AndroidRuntime(4082):     at android.os.Handler.handleCallback(Handler.java:730)
05-17 05:54:59.356: E/AndroidRuntime(4082):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-17 05:54:59.356: E/AndroidRuntime(4082):     at android.os.Looper.loop(Looper.java:137)
05-17 05:54:59.356: E/AndroidRuntime(4082):     at android.app.ActivityThread.main(ActivityThread.java:5103)
05-17 05:54:59.356: E/AndroidRuntime(4082):     at java.lang.reflect.Method.invokeNative(Native Method)
05-17 05:54:59.356: E/AndroidRuntime(4082):     at java.lang.reflect.Method.invoke(Method.java:525)
05-17 05:54:59.356: E/AndroidRuntime(4082):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-17 05:54:59.356: E/AndroidRuntime(4082):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-17 05:54:59.356: E/AndroidRuntime(4082):     at dalvik.system.NativeStart.main(Native Method)

I know the files are too large but there must be a way right.

1
  • 1
    Instead of trying to allocate an array the size of the file, allocate an array of a manageable size, e.g. 2MB and use a loop that processes the file one chunk at a time, writing the chunks to a file output stream as you go along. Commented May 17, 2014 at 10:18

1 Answer 1

1

Instead of using a FileInputStream directly, wrap that in a BufferedInputStream, so that you don't allocate huge amounts of memory. A buffered input stream reads the data in segments, so you can encrypt that piece, and, if you are writing it back to a file, use a BufferedWriter to write that file in the same way.

Sign up to request clarification or add additional context in comments.

3 Comments

The important part is to not try to fit it all into a byte[] at once. Work on smaller portions of the file at a time, or do all of your operations using the input/output streams themselves.
can you please elaborate the method a little. I guess I need to create the byte array for that too, the error is on the line where I am creating the byte[].
@Sourabh No, you don't need to create a byte array. Create a BufferedInputStream by using a new BufferedInputStream(new FileInputStream()) (with the parameters that you've used). Have a look a the BufferedInputStream JavaDoc. Inside the BufferedInputStream, it reads the data in small segments, which is how you should process your data, instead of one huge byte[].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.