fix(file): resolveBaseDir 多重尝试创建目录,兼容华为/小米设备

- mkdirs() 静默失败时,尝试 Runtime.exec('mkdir -p')
- 仍失败则回退到 Sandbox 目录,避免 ENOENT 崩溃
这个提交包含在:
XuqmGroup 2026-06-18 13:50:14 +08:00
父节点 aeeba4fac8
当前提交 a6b1905a66

查看文件

@ -278,12 +278,25 @@ object FileSDK {
private fun resolveBaseDir(context: Context, destination: FileDownloadDestination): File = when (destination) { private fun resolveBaseDir(context: Context, destination: FileDownloadDestination): File = when (destination) {
FileDownloadDestination.PublicDownloads -> { FileDownloadDestination.PublicDownloads -> {
val appName = context.applicationInfo.loadLabel(context.packageManager).toString() val appName = context.applicationInfo.loadLabel(context.packageManager).toString()
File( val dir = File(
android.os.Environment.getExternalStoragePublicDirectory( android.os.Environment.getExternalStoragePublicDirectory(
android.os.Environment.DIRECTORY_DOWNLOADS android.os.Environment.DIRECTORY_DOWNLOADS
), ),
appName, appName,
).apply { mkdirs() } )
// mkdirs() 在部分华为/小米设备上静默失败,多重尝试确保目录存在
if (!dir.exists()) {
dir.mkdirs()
}
if (!dir.exists()) {
runCatching { Runtime.getRuntime().exec(arrayOf("mkdir", "-p", dir.absolutePath)).waitFor() }
}
if (!dir.exists()) {
android.util.Log.w("XWV", "resolveBaseDir: cannot create ${dir.absolutePath}, falling back to Sandbox")
context.getExternalFilesDir(null) ?: context.filesDir
} else {
dir
}
} }
FileDownloadDestination.Sandbox -> { FileDownloadDestination.Sandbox -> {
context.getExternalFilesDir(null) ?: context.filesDir context.getExternalFilesDir(null) ?: context.filesDir