handle renaming files on Android 30
This commit is contained in:
parent
9759404390
commit
39350e0a86
2 changed files with 46 additions and 7 deletions
|
@ -55,10 +55,12 @@ abstract class BaseSimpleActivity : AppCompatActivity() {
|
|||
private val GENERIC_PERM_HANDLER = 100
|
||||
private val DELETE_FILE_SDK_30_HANDLER = 300
|
||||
private val RECOVERABLE_SECURITY_HANDLER = 301
|
||||
private val UPDATE_FILE_SDK_30_HANDLER = 302
|
||||
|
||||
companion object {
|
||||
var funAfterSAFPermission: ((success: Boolean) -> Unit)? = null
|
||||
var funAfterDelete30File: ((success: Boolean) -> Unit)? = null
|
||||
var funAfterUpdate30File: ((success: Boolean) -> Unit)? = null
|
||||
var funRecoverableSecurity: ((success: Boolean) -> Unit)? = null
|
||||
}
|
||||
|
||||
|
@ -304,6 +306,8 @@ abstract class BaseSimpleActivity : AppCompatActivity() {
|
|||
} else if (requestCode == RECOVERABLE_SECURITY_HANDLER) {
|
||||
funRecoverableSecurity?.invoke(resultCode == Activity.RESULT_OK)
|
||||
funRecoverableSecurity = null
|
||||
} else if (requestCode == UPDATE_FILE_SDK_30_HANDLER) {
|
||||
funAfterUpdate30File?.invoke(resultCode == Activity.RESULT_OK)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -452,6 +456,21 @@ abstract class BaseSimpleActivity : AppCompatActivity() {
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
fun updateSDK30Uris(uris: List<Uri>, callback: (success: Boolean) -> Unit) {
|
||||
if (isRPlus()) {
|
||||
funAfterUpdate30File = callback
|
||||
try {
|
||||
val writeRequest = MediaStore.createWriteRequest(contentResolver, uris).intentSender
|
||||
startIntentSenderForResult(writeRequest, UPDATE_FILE_SDK_30_HANDLER, null, 0, 0, 0)
|
||||
} catch (e: Exception) {
|
||||
showErrorToast(e)
|
||||
}
|
||||
} else {
|
||||
callback(false)
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
fun handleRecoverableSecurityException(callback: (success: Boolean) -> Unit) {
|
||||
try {
|
||||
|
|
|
@ -793,7 +793,31 @@ fun BaseSimpleActivity.renameFile(oldPath: String, newPath: String, callback: ((
|
|||
} else {
|
||||
val oldFile = File(oldPath)
|
||||
val newFile = File(newPath)
|
||||
val tempFile = createTempFile(oldFile) ?: return
|
||||
val tempFile = try {
|
||||
createTempFile(oldFile) ?: return
|
||||
} catch (exception: Exception) {
|
||||
if (isRPlus() && exception is java.nio.file.FileSystemException) {
|
||||
val fileUris = getFileUrisFromFileDirItems(arrayListOf(File(oldPath).toFileDirItem(this)))
|
||||
updateSDK30Uris(fileUris) {
|
||||
val values = ContentValues().apply {
|
||||
put(MediaStore.Images.Media.DISPLAY_NAME, newPath.getFilenameFromPath())
|
||||
}
|
||||
|
||||
try {
|
||||
contentResolver.update(fileUris.first(), values, null, null)
|
||||
callback?.invoke(true)
|
||||
} catch (e: Exception) {
|
||||
showErrorToast(e)
|
||||
callback?.invoke(false)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
showErrorToast(exception)
|
||||
callback?.invoke(false)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
val oldToTempSucceeds = oldFile.renameTo(tempFile)
|
||||
val tempToNewSucceeds = tempFile.renameTo(newFile)
|
||||
if (oldToTempSucceeds && tempToNewSucceeds) {
|
||||
|
@ -830,12 +854,8 @@ fun Activity.createTempFile(file: File): File? {
|
|||
createTempDir("temp", "${System.currentTimeMillis()}", file.parentFile)
|
||||
} else {
|
||||
if (isRPlus()) {
|
||||
try {
|
||||
kotlin.io.path.createTempFile(file.parentFile.toPath(), "temp", "${System.currentTimeMillis()}").toFile()
|
||||
} catch (e: Exception) {
|
||||
showErrorToast(e)
|
||||
null
|
||||
}
|
||||
// this can throw FileSystemException, lets catch and handle it at the place calling this function
|
||||
kotlin.io.path.createTempFile(file.parentFile.toPath(), "temp", "${System.currentTimeMillis()}").toFile()
|
||||
} else {
|
||||
createTempFile("temp", "${System.currentTimeMillis()}", file.parentFile)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue