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.PermissionManager.getGrantedDirForPath
import deckers.thibault.aves.utils.UriUtils.tryParseId import deckers.thibault.aves.utils.UriUtils.tryParseId
import java.io.File import java.io.File
import java.io.FileNotFoundException
import java.io.InputStream import java.io.InputStream
import java.io.OutputStream import java.io.OutputStream
import java.util.* import java.util.*
@ -404,37 +403,37 @@ object StorageUtils {
// returns the directory `DocumentFile` (from tree URI when scoped storage is required, `File` otherwise) // 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 // returns null if directory does not exist and could not be created
fun createDirectoryDocIfAbsent(context: Context, dirPath: String): DocumentFileCompat? { fun createDirectoryDocIfAbsent(context: Context, dirPath: String): DocumentFileCompat? {
val cleanDirPath = ensureTrailingSeparator(dirPath) try {
return if (requireAccessPermission(context, cleanDirPath) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { val cleanDirPath = ensureTrailingSeparator(dirPath)
val grantedDir = getGrantedDirForPath(context, cleanDirPath) ?: return null return if (requireAccessPermission(context, cleanDirPath) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val rootTreeDocumentUri = convertDirPathToTreeDocumentUri(context, grantedDir) ?: return null val grantedDir = getGrantedDirForPath(context, cleanDirPath) ?: return null
var parentFile: DocumentFileCompat? = DocumentFileCompat.fromTreeUri(context, rootTreeDocumentUri) ?: return null val rootTreeDocumentUri = convertDirPathToTreeDocumentUri(context, grantedDir) ?: return null
val pathIterator = getPathStepIterator(context, cleanDirPath, grantedDir) var parentFile: DocumentFileCompat? = DocumentFileCompat.fromTreeUri(context, rootTreeDocumentUri) ?: return null
while (pathIterator?.hasNext() == true) { val pathIterator = getPathStepIterator(context, cleanDirPath, grantedDir)
val dirName = pathIterator.next() while (pathIterator?.hasNext() == true) {
var dirFile = findDocumentFileIgnoreCase(parentFile, dirName) val dirName = pathIterator.next()
if (dirFile == null || !dirFile.exists()) { var dirFile = findDocumentFileIgnoreCase(parentFile, dirName)
try { if (dirFile == null || !dirFile.exists()) {
dirFile = parentFile?.createDirectory(dirName) dirFile = parentFile?.createDirectory(dirName)
if (dirFile == null) { if (dirFile == null) {
Log.e(LOG_TAG, "failed to create directory with name=$dirName from parent=$parentFile") Log.e(LOG_TAG, "failed to create directory with name=$dirName from parent=$parentFile")
return null return null
} }
} catch (e: FileNotFoundException) {
Log.e(LOG_TAG, "failed to create directory with name=$dirName from parent=$parentFile", e)
return null
} }
parentFile = dirFile
} }
parentFile = dirFile parentFile
} else {
val directory = File(cleanDirPath)
if (!directory.exists() && !directory.mkdirs()) {
Log.e(LOG_TAG, "failed to create directories at path=$cleanDirPath")
return null
}
DocumentFileCompat.fromFile(directory)
} }
parentFile } catch (e: Exception) {
} else { Log.e(LOG_TAG, "failed to create directory at path=$dirPath", e)
val directory = File(cleanDirPath) return null
if (!directory.exists() && !directory.mkdirs()) {
Log.e(LOG_TAG, "failed to create directories at path=$cleanDirPath")
return null
}
DocumentFileCompat.fromFile(directory)
} }
} }