From 371f88fe369a97ff0ac9a6cfc061cf2653063dad Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 1 Aug 2019 18:17:24 +0200 Subject: [PATCH] do not show an error at renaming a single SD card file if it succeeds --- .../commons/extensions/Activity.kt | 24 +++++++++---------- .../commons/extensions/Context-storage.kt | 2 +- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt index a1bfd053c..16e51d48a 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity.kt @@ -541,21 +541,19 @@ fun BaseSimpleActivity.renameFile(oldPath: String, newPath: String, callback: (( } try { - val uri = DocumentsContract.renameDocument(applicationContext.contentResolver, document.uri, newPath.getFilenameFromPath()) - if (document.uri != uri) { - updateInMediaStore(oldPath, newPath) - rescanPaths(arrayListOf(oldPath, newPath)) { - if (!baseConfig.keepLastModified) { - updateLastModified(newPath, System.currentTimeMillis()) - } - deleteFromMediaStore(oldPath) - runOnUiThread { - callback?.invoke(true) - } + try { + DocumentsContract.renameDocument(applicationContext.contentResolver, document.uri, newPath.getFilenameFromPath()) + } catch (ignored: FileNotFoundException) { + // FileNotFoundException is thrown in some weird cases, but renaming works just fine + } + updateInMediaStore(oldPath, newPath) + rescanPaths(arrayListOf(oldPath, newPath)) { + if (!baseConfig.keepLastModified) { + updateLastModified(newPath, System.currentTimeMillis()) } - } else { + deleteFromMediaStore(oldPath) runOnUiThread { - callback?.invoke(false) + callback?.invoke(true) } } } catch (e: Exception) { diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-storage.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-storage.kt index 5b0fb96fb..157518a2b 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-storage.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-storage.kt @@ -221,7 +221,7 @@ fun Context.getDocumentFile(path: String): DocumentFile? { for (part in parts) { document = document?.findFile(part) } - return document + document } catch (ignored: Exception) { null }