Wednesday, September 4, 2013

Android MediaRecorder.setMaxFileSize Exception due to file size

Android Media Recorder will stop functioning and show a Java.lang.RuntimeException if we provide a very small file size, for example if you will put 1024 as the MediaRecorder.setMaxFileSize parameter, the recording won't even start in the first place. Remember, the number provided as input to this method represents maximum number of bytes which would be stored in a file by the MediaRecorder.
Android MediaRecorder

The reason is that it takes almost 10 KB to store a audio recording of 1 second or so. When you will put 1024 bytes as a parameter, the MediaRecord will simple run out of space when it will try to stop the voice recording upon reaching the tiny number 1024 bytes. The error code returned by such a situation is -22, and the logging line will look something like the one given below: