This IOException started happening in my Android app after upgrading to AndroidX and increasing the target SDK to version 28.
Previous to this, the same code was working fine.
It is a Capacitor app, and uses the capacitor-video-recorder plugin. Under the hood, this plugin uses the fancycamera java library which interacts with android.media.MediaRecorder.
Here is the stack trace, which is thrown when calling VideoRecorder.startRecording, eventually leading to the call to MediaRecorder.prepare:
I/IMediaRecorder: prepare (BpMediaRecorder client) in file frameworks/av/media/libmedia/IMediaRecorder.cpp, function prepare, line 253
E/MediaRecorder: prepare failed: -2147483648
W/System.err: java.io.IOException: prepare failed.
W/System.err: at android.media.MediaRecorder._prepare(Native Method)
W/System.err: at android.media.MediaRecorder.prepare(MediaRecorder.java:1038)
W/System.err: at co.fitcom.fancycamera.Camera2.setUpMediaRecorder(Camera2.java:607)
W/System.err: at co.fitcom.fancycamera.Camera2.startRecording(Camera2.java:837)
W/System.err: at co.fitcom.fancycamera.FancyCamera.startRecording(FancyCamera.java:323)
W/System.err: at com.github.sbannigan.capacitor.VideoRecorder.startRecording(VideoRecorder.java:267)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:99)
W/System.err: at com.getcapacitor.Bridge$1.run(Bridge.java:515)
W/System.err: at android.os.Handler.handleCallback(Handler.java:907)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:105)
W/System.err: at android.os.Looper.loop(Looper.java:216)
W/System.err: at android.os.HandlerThread.run(HandlerThread.java:65)
The Android app is requesting CAMERA and RECORD_AUDIO permissions, and I can confirm that the user is prompted for these first.
AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
I can also see that an output file is created in the app's data directory. However the file remains empty. I've also manually tried writing to the same file and it is possible, so file IO does not appear to be the problem.
The crash is coming from a Native Method, so the debugger isn't very helpful. However I can at least confirm that MediaRecorder's mPath variable appears to be set correctly just before the call to _prepare.
I have created an example app with an identical stack, where it's possible to recreate the error: https://github.com/disbelief/video-recorder-test
I'd also be interested to hear what other possible reasons there might be for MediaRecorder.prepare to throw this exception.
June, 2022
The mistake in my case was very simple. Some phones have Camera resolution as screen size by default but others dont. That is why on many devices it throws
prepare failed: -2147483648That MediaRecorder in depth is that camera itself. Thus you need to pass allowed size . Obviously at best the maximum (tomMediaRecorder.setVideoSize())and CameraPropeties class:
}