Friday, 3 December 2021

ErrnoException: open failed: EPERM (Operation not permitted) in samsung android

I am simply write into file which is created in internal storage. It's working fine in most of the devices but strangely it's not working on few Samsung devices with Android 11 version.

I am using all three permissions:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="30"/>
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
        android:minSdkVersion="30"/>

Below is my code part for writing into file:

val oldfile = File(oldBasePath)
val fiStream = FileInputStream(oldfile)
var outputFile = File(appFolder, fileName)
var fos = FileOutputStream(outputFile)
val buf = ByteArray(1024)
var len: Int
    do {
           len = fiStream.read(buf)
           if (len > 0) {
                  fos?.write(buf, 0, len)
                 } else {
                     break
                  }
                 } while (true)

                fiStream.close()
                fos?.close()

Here I am getting error in 2nd line of code. Below is the logcat:

Non-fatal Exception: java.io.FileNotFoundException: /storage/emulated/0/MyFileStore/Röst-inspelning.txt: open failed: EPERM (Operation not permitted)
       at libcore.io.IoBridge.open(IoBridge.java:492)
       at java.io.FileOutputStream.<init>(FileOutputStream.java:236)
       at java.io.FileOutputStream.<init>(FileOutputStream.java:186)
       at com.test.ui.FileListFragment.saveFileApps(FileListFragment.java:729)
       at com.test.ui.FileListFragment.access$saveFileApps(FileListFragment.java:50)
       at com.test.ui.FileListFragment$loadAppFiles$1$result1$1.invokeSuspend(FileListFragment.java:541)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(BaseContinuationImpl.java:33)
       at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.java:106)
       at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.java:571)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.java:750)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.java:678)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.java:665)

Caused by android.system.ErrnoException: open failed: EPERM (Operation not permitted)
       at libcore.io.Linux.open(Linux.java)
       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:8494)
       at libcore.io.IoBridge.open(IoBridge.java:478)
       at java.io.FileOutputStream.<init>(FileOutputStream.java:236)
       at java.io.FileOutputStream.<init>(FileOutputStream.java:186)
       at com.test.ui.FileListFragment.saveFileApps(FileListFragment.java:729)
       at com.test.ui.FileListFragment.access$saveFileApps(FileListFragment.java:50)
       at com.test.ui.FileListFragment$loadAppFiles$1$result1$1.invokeSuspend(FileListFragment.java:541)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(BaseContinuationImpl.java:33)
       at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.java:106)
       at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.java:571)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.java:750)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.java:678)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.java:665)


from ErrnoException: open failed: EPERM (Operation not permitted) in samsung android

No comments:

Post a Comment