'FileNotFoundException when app is updated to Android 11+ MediaRecord.prepare(). "FileNotFoundException open failed: EPERM (Operation not permitted)"

When you install the app for the first time, it works normally without any problems. However, if you update the app while the app is already installed, a problem occurs in api 30+ version.(current .apk, update .aab)

java.io.FileNotFoundException:/storage/emulated/0/Documents/xxxx/100536_20220503_092934_96001.mp4: open failed: EPERM (Operation not permitted)
    at libcore.io.IoBridge.open(IoBridge.java:492)
    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:289)
    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:152)
    at android.media.MediaRecorder.prepare(MediaRecorder.java:1417)
    at xxxx.xxxx.com.xxxx.service.RecordService.onStart(RecordService.java:207)
    at android.app.Service.onStartCommand(Service.java:505)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4795)
    at android.app.ActivityThread.access$2000(ActivityThread.java:301)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2215)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8653)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: android.system.ErrnoException: open failed: EPERM (Operation not permitted)
    at libcore.io.Linux.open(Native Method)
    at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
    at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
    at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8514)
    at libcore.io.IoBridge.open(IoBridge.java:478)

'''



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source