fixed crash from IllegalStateException on mkdir

This commit is contained in:
Thibault Deckers 2022-05-17 10:19:56 +09:00
parent 3ba7940d27
commit 65ec62c117

View file

@ -21,7 +21,6 @@ import deckers.thibault.aves.utils.MimeTypes.isVideo
import deckers.thibault.aves.utils.PermissionManager.getGrantedDirForPath
import deckers.thibault.aves.utils.UriUtils.tryParseId
import java.io.File
import java.io.FileNotFoundException
import java.io.InputStream
import java.io.OutputStream
import java.util.*
@ -404,6 +403,7 @@ object StorageUtils {
// returns the directory `DocumentFile` (from tree URI when scoped storage is required, `File` otherwise)
// returns null if directory does not exist and could not be created
fun createDirectoryDocIfAbsent(context: Context, dirPath: String): DocumentFileCompat? {
try {
val cleanDirPath = ensureTrailingSeparator(dirPath)
return if (requireAccessPermission(context, cleanDirPath) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val grantedDir = getGrantedDirForPath(context, cleanDirPath) ?: return null
@ -414,16 +414,11 @@ object StorageUtils {
val dirName = pathIterator.next()
var dirFile = findDocumentFileIgnoreCase(parentFile, dirName)
if (dirFile == null || !dirFile.exists()) {
try {
dirFile = parentFile?.createDirectory(dirName)
if (dirFile == null) {
Log.e(LOG_TAG, "failed to create directory with name=$dirName from parent=$parentFile")
return null
}
} catch (e: FileNotFoundException) {
Log.e(LOG_TAG, "failed to create directory with name=$dirName from parent=$parentFile", e)
return null
}
}
parentFile = dirFile
}
@ -436,6 +431,10 @@ object StorageUtils {
}
DocumentFileCompat.fromFile(directory)
}
} catch (e: Exception) {
Log.e(LOG_TAG, "failed to create directory at path=$dirPath", e)
return null
}
}
private fun getDocumentFileFromVolumeTree(context: Context, rootTreeDocumentUri: Uri, anyPath: String): DocumentFileCompat? {