title: MediaRecorder.prepare() - FileNotFoundException from time to time for some users and specific devices
body: <p>I have an app which records videos, it has about 80K current installations (more than 100K downloads), Crashlytics statistics seems to be good - Crash-free users 99.66% and rating on Google Play is also OK - 4.5</p>
  <p>But still for some of my users can happen the next error (it may happened for each user once or a couple of times by now, so not often, but still happens from time to time):</p>
  <blockquote>
  <p>java.io.FileNotFoundException:
  /storage/7886-1D16/Android/media/MY_APP_PACKAGE_NAME_HERE/2021-03-18
  21h 18m 06s.mp4: open failed: EROFS ... blah blah blah ... android.media.MediaRecorder.prepare(MediaRecorder.java:1028)</p>
  </blockquote>
  <p>Here's the code (similar to real one in my project), as you can see before <code>mediaRecorder.prepare()</code> and video recording I check if output directory is good, if <code>File.canWrite()</code> returns <code>true</code> then it means that files exists, app has needed permission to write at this path and storage is mounted <a href="https://developer.android.com/reference/java/io/File#canWrite()" rel="nofollow noreferrer">https://developer.android.com/reference/java/io/File#canWrite()</a></p>
  <pre><code>// canWrite() - means that file exists and app is allowed to write
  if (outputFolderFile.canWrite()) {
      // directory is OK, Android says that there is nothing wrong...
      
      val videoFile = File(outputFolderFile, videoFileName)
      
      ... // MediaRecorder settings
      
      mediaRecorder.setOutputFile(videoFile.absolutePath)
      
      ... // other MediaRecorder settings
      
      try {
          mediaRecorder.prepare() // FileNotFoundException here from time to time
      } catch (e: Exception) {
          e.printStackTrace()
          
          openReportErrorDialog(e)
      }
  }
  </code></pre>
  <p>Additionally I was trying to add <code>&amp;&amp; Environment.getExternalStorageState(outputFolderFile).equals(Environment.MEDIA_MOUNTED))</code> or <code>|| (outputFolderFile.mkdirs() &amp;&amp; outputFolderFile.canWrite())</code> to that condition (<code>outputFolderFile.canWrite()</code>) but it doesn't matter, error still happens</p>
  <p>I also was trying to add <code>createNewFile(videoFile )</code> line before <code>mediaRecorder.prepare()</code> but doesn't help and in this case the error is <code>java.io.IOException: No such file or directory, java.io.IOException: No such file or directory at java.io.UnixFileSystem.createFileExclusively0(Native Method)</code></p>
  <p>So Android says that directory exists, everything is ok and app can write files there but...</p>
  <p>Here I almost added all reports from my users about this error:</p>
  <pre><code>HUAWEI CAG-L22
  java.io.FileNotFoundException: /storage/7886-1D16/Android/media/MY_APP_PACKAGE_NAME_HERE/2021-03-18 21h 18m 06s.mp4: open failed: EROFS (Read-only file system)
  
  HUAWEI FIG-LX1
  java.io.FileNotFoundException: /storage/5309-1D08/Android/media/MY_APP_PACKAGE_NAME_HERE/2021-04-10/2021-04-10 10h 17m 18s.mp4: open failed: EROFS (Read-only file system)
  
  LGE LG-K350
  java.io.FileNotFoundException: /storage/4CA1-71F9/Android/media/MY_APP_PACKAGE_NAME_HERE/2021-04-02/2021-04-02 14h 53m 31s.mp4: open failed: ENOENT (No such file or directory)
  
  LGE LM-K300
  java.io.FileNotFoundException: /storage/C5F6-15E4/Android/media/MY_APP_PACKAGE_NAME_HERE/2021-03-06/2021-03-06 02h 26m 40s PM.mp4: open failed: ENOENT (No such file or directory)
   
  LGE LM-X420
  java.io.FileNotFoundException: /storage/B15A-170A/Android/media/MY_APP_PACKAGE_NAME_HERE/2021-02-12/2021-02-12 13h 31m 42s.mp4: open failed: ENOENT (No such file or directory)
  
  OPPO CPH1931
  java.io.FileNotFoundException: /storage/02EB-1B18/Android/media/MY_APP_PACKAGE_NAME_HERE/2021-03-13/2021-03-13 12h 15m 30s.mp4: open failed: EROFS (Read-only file system)
  
  Prestigio PMT3257_3G
  java.io.FileNotFoundException: /storage/emulated/0/Android/media/MY_APP_PACKAGE_NAME_HERE/2021-04-10/2021-04-10 10h 32m 21s.mp4: open failed: ENOENT (No such file or directory),
  
  samsung SM-G900F
  java.io.FileNotFoundException: /storage/161A-0B3C/Android/media/MY_APP_PACKAGE_NAME_HERE/2021-03-02/2021-03-02 08h 06m 33s.mp4: open failed: ENOENT (No such file or directory)
  
  samsung SM-N960W
  java.io.FileNotFoundException: /storage/2A2C-40A4/Android/media/MY_APP_PACKAGE_NAME_HERE/2021-04-14/2021-04-14 02h 35m 45s AM.mp4: open failed: ENOENT (No such file or directory)
  
  Xiaomi M2004J19C
  java.io.FileNotFoundException: /storage/emulated/0/Android/media/MY_APP_PACKAGE_NAME_HERE/2021-03-11 11h 09m 18s AM.mp4: open failed: ENOENT (No such file or directory)
  
  Xiaomi M2007J20CG
  java.io.FileNotFoundException: /storage/82AD-49E3/Android/media/MY_APP_PACKAGE_NAME_HERE/2021-04-06/2021-04-06 06h 51m 14s.mp4: open failed: ENOENT (No such file or directory)
  
  Xiaomi M2007J3SG
  java.io.FileNotFoundException: /storage/emulated/0/Movies/MY_APP_NAME_HERE/2021-03-31/2021-03-31 15h 39m 27s.mp4: open failed: EPERM (Operation not permitted)
  
  Xiaomi M2007J3SY
  java.io.FileNotFoundException: /storage/emulated/0/Movies/MY_APP_NAME_HERE/2021-04-10/2021-04-10 01h 30m 42s PM.mp4: open failed: EPERM (Operation not permitted)
  
  Xiaomi M2102J20SG
  java.io.FileNotFoundException: /storage/emulated/0/Movies/MY_APP_NAME_HERE/2021-04-17/2021-04-17 16h 16m 59s.mp4: open failed: EPERM (Operation not permitted)
  
  Xiaomi POCO F2 Pro
  java.io.FileNotFoundException: /storage/emulated/0/Movies/MY_APP_NAME_HERE/2021-02-05/2021-02-05 19h 20m 36s.mp4: open failed: EPERM (Operation not permitted)
  
  Xiaomi Redmi 5A
  java.io.FileNotFoundException: /storage/577D-D95B/Android/media/MY_APP_PACKAGE_NAME_HERE/2021-03-13/2021-03-13 16h 33m 21s.mp4: open failed: EROFS (Read-only file system)
  
  Xiaomi Redmi 7A
  java.io.FileNotFoundException: /storage/4DC6-1515/Android/media/MY_APP_PACKAGE_NAME_HERE/2021-03-12/2021-03-12 09h 18m 35s.mp4: open failed: EIO (I/O error)
  
  Xiaomi Redmi Note 8T
  java.io.FileNotFoundException: /storage/A018-ED55/Android/media/MY_APP_PACKAGE_NAME_HERE/2021-02-06/2021-02-06 17h 29m 59s.mp4: open failed: EIO (I/O error)
  
  ZTE A2017G
  java.io.FileNotFoundException: /storage/emulated/0/Movies/MY_APP_NAME_HERE/2021-03-26/2021-03-26 08h 27m 42s.mp4: open failed: ENOENT (No such file or directory)
  </code></pre>
  <p>So it mostly happens for Xiaomi devices, and mostly when recording to external micro SD Card (e.g. path is <code>/storage/2A2C-40A4/***</code>), though for built-in storage of phone (<code>/storage/emulated/0/***</code>) it also happens sometimes...</p>
  <p>Versions of Android are different, from 6 (old) to 11 (newest), so it doesn't matter.</p>
  <p>You can see that error is similar (starting with <code>FileNotFoundException</code> and file absolute path) but sometimes it changes at the end between</p>
  <ul>
  <li><code>open failed: EROFS (Read-only file system)</code></li>
  <li><code>open failed: ENOENT (No such file or directory)</code></li>
  <li><code>open failed: EIO (I/O error)</code></li>
  <li><code>open failed: EPERM (Operation not permitted)</code></li>
  </ul>
  <p>Of course users don't send such error report everyday but still it happens often.</p>
  <p>I don't have any idea why it happens. I can't reproduce this issue on my own devices (two Samsung, one Lenovo, one Huawei and one Xiaomi devices).</p>
  <p>Maybe it happens because read/write time to storage was very slow or freezed (heavy load on it storage controller) at this point and Android (MediaRecorder) could not create an output video file for recording?</p>
  <p>Any ideas or solutions?</p>
  <p>p.s. full example of error:</p>
  <blockquote>
  <p>java.io.FileNotFoundException:
  /storage/5309-1D08/Android/media/MY_APP_PACKAGE_NAME_HERE/2021-04-10/2021-04-10
  10h 17m 20s.mp4: open failed: EROFS (Read-only file system) at
  libcore.io.IoBridge.open(IoBridge.java:485) at
  java.io.RandomAccessFile.(RandomAccessFile.java:288) at
  java.io.RandomAccessFile.(RandomAccessFile.java:151) at
  android.hsm.HwMediaPermWrapper.setOutputFile(HwMediaPermWrapper.java:127)
  at android.media.MediaRecorder.prepare(MediaRecorder.java:1028)</p>
  </blockquote>