fixed crash from IllegalStateException on mkdir
This commit is contained in:
parent
3ba7940d27
commit
65ec62c117
1 changed files with 23 additions and 24 deletions
|
@ -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,6 +403,7 @@ 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? {
|
||||||
|
try {
|
||||||
val cleanDirPath = ensureTrailingSeparator(dirPath)
|
val cleanDirPath = ensureTrailingSeparator(dirPath)
|
||||||
return if (requireAccessPermission(context, cleanDirPath) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
return if (requireAccessPermission(context, cleanDirPath) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||||
val grantedDir = getGrantedDirForPath(context, cleanDirPath) ?: return null
|
val grantedDir = getGrantedDirForPath(context, cleanDirPath) ?: return null
|
||||||
|
@ -414,16 +414,11 @@ object StorageUtils {
|
||||||
val dirName = pathIterator.next()
|
val dirName = pathIterator.next()
|
||||||
var dirFile = findDocumentFileIgnoreCase(parentFile, dirName)
|
var dirFile = findDocumentFileIgnoreCase(parentFile, dirName)
|
||||||
if (dirFile == null || !dirFile.exists()) {
|
if (dirFile == null || !dirFile.exists()) {
|
||||||
try {
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
@ -436,6 +431,10 @@ object StorageUtils {
|
||||||
}
|
}
|
||||||
DocumentFileCompat.fromFile(directory)
|
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? {
|
private fun getDocumentFileFromVolumeTree(context: Context, rootTreeDocumentUri: Uri, anyPath: String): DocumentFileCompat? {
|
||||||
|
|
Loading…
Reference in a new issue