在 Android 11 中访问 USB 存储(OTG)

问题描述 投票:0回答:1

根据文档 https://developer.android.com/training/data-storage/manage-all-files#operations-allowed-manage-external-storage,MANAGE_EXTERNAL_STORAGE 权限授予对 USB (OTG) 驱动器根目录的访问权限。

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

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

上面的代码适用于主存储和 SDCARD。对于 USB OTG 驱动器,这将返回以下错误消息:

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)

请帮我看看我的错误在哪里?

android storage file-permissions android-external-storage
1个回答
-1
投票

我在以下位置报告了此事 Google 问题跟踪器。
并得到答复“问题已在 S 版本中修复”。已检查,其工作 Android 12 及以上版本

© www.soinside.com 2019 - 2024. All rights reserved.