Search code examples
androidstoragefile-permissionsandroid-external-storage

Access USB Storage (OTG) in android 11


According to the documentation https://developer.android.com/training/data-storage/manage-all-files#operations-allowed-manage-external-storage, MANAGE_EXTERNAL_STORAGE permission grant access to root directory of the USB (OTG) drive.

val sm = application.getSystemService(Context.STORAGE_SERVICE) as StorageManager
val sv = sm.storageVolumes

sv.forEach{
    val curVolStats = StatFs(it.directory.toString())
}

The above code works well for Primary Storage and SDCARD. For the USB OTG drive, this return below error message:

2022-09-14 11:03:28.456 14141-14141/com.starsolutions.starfilemanager E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.starsolutions.starfilemanager, PID: 14141
    java.lang.IllegalArgumentException: Invalid path: /mnt/media_rw/7ACF-B2AD
        at android.os.StatFs.doStat(StatFs.java:53)
        at android.os.StatFs.<init>(StatFs.java:43)
        at com.starsolutions.starfilemanager.data.ExplorerDataSource.getVolumes(ExplorerDataSource.kt:36)
        at com.starsolutions.starfilemanager.data.ExplorerRepository$getVolumeDetails$2.invokeSuspend(ExplorerRepository.kt:20)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
        at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:749)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
        Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@d17c76f, Dispatchers.Main.immediate]
    Caused by: android.system.ErrnoException: statvfs failed: EACCES (Permission denied)

Please help me where is my mistake?


Solution

  • I reported this in Google issue tracker.
    and got reply 'issue is fixed in S build'. Checked and its working android 12 and above