fix #1946, properly handle overwriting edited files on an SD card

This commit is contained in:
tibbi 2020-10-28 18:47:18 +01:00
parent ba4ba6a338
commit 736416c26c

View file

@ -13,10 +13,12 @@ import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.helpers.REAL_FILE_PATH import com.simplemobiletools.commons.helpers.REAL_FILE_PATH
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isNougatPlus import com.simplemobiletools.commons.helpers.isNougatPlus
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.R
import com.simplemobiletools.gallery.pro.dialogs.SaveAsDialog import com.simplemobiletools.gallery.pro.dialogs.SaveAsDialog
import com.simplemobiletools.gallery.pro.extensions.config import com.simplemobiletools.gallery.pro.extensions.config
import com.simplemobiletools.gallery.pro.extensions.fixDateTaken import com.simplemobiletools.gallery.pro.extensions.fixDateTaken
import com.simplemobiletools.gallery.pro.extensions.tryDeleteFileDirItem
import ly.img.android.pesdk.PhotoEditorSettingsList import ly.img.android.pesdk.PhotoEditorSettingsList
import ly.img.android.pesdk.assets.filter.basic.FilterPackBasic import ly.img.android.pesdk.assets.filter.basic.FilterPackBasic
import ly.img.android.pesdk.assets.font.basic.FontPackBasic import ly.img.android.pesdk.assets.font.basic.FontPackBasic
@ -134,6 +136,7 @@ class NewEditActivity : SimpleActivity() {
storeOldExif(source) storeOldExif(source)
sourceFileLastModified = File(source).lastModified() sourceFileLastModified = File(source).lastModified()
handleFileOverwriting(destinationFilePath) {
var inputStream: InputStream? = null var inputStream: InputStream? = null
var outputStream: OutputStream? = null var outputStream: OutputStream? = null
try { try {
@ -172,6 +175,7 @@ class NewEditActivity : SimpleActivity() {
outputStream?.close() outputStream?.close()
} }
} }
}
} else { } else {
toast(R.string.image_editing_failed) toast(R.string.image_editing_failed)
finish() finish()
@ -197,6 +201,23 @@ class NewEditActivity : SimpleActivity() {
} }
} }
// in case the user wants to overwrite the original file and it is on an SD card, delete it manually. Else the system just appends (1)
private fun handleFileOverwriting(path: String, callback: () -> Unit) {
if (getDoesFilePathExist(path) && isPathOnSD(path)) {
val fileDirItem = FileDirItem(path, path.getFilenameFromPath())
tryDeleteFileDirItem(fileDirItem, false, true) { success ->
if (success) {
callback()
} else {
toast(R.string.unknown_error_occurred)
finish()
}
}
} else {
callback()
}
}
private fun openEditor(inputImage: Uri) { private fun openEditor(inputImage: Uri) {
val settingsList = createPesdkSettingsList() val settingsList = createPesdkSettingsList()