From 5da2c82f471b0dbdd4d4b3c3e70ef58c7591fba0 Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 13 Apr 2022 18:45:49 -0400 Subject: [PATCH] Avoid crashing if picture can't be saved (related to #6905) --- .../kanade/tachiyomi/data/saver/ImageSaver.kt | 23 +++++++++++-------- app/src/main/res/values/strings.xml | 1 + 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/saver/ImageSaver.kt b/app/src/main/java/eu/kanade/tachiyomi/data/saver/ImageSaver.kt index 5314ccd627..673b936549 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/saver/ImageSaver.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/saver/ImageSaver.kt @@ -13,6 +13,8 @@ import eu.kanade.tachiyomi.util.storage.DiskUtil import eu.kanade.tachiyomi.util.storage.cacheImageDir import eu.kanade.tachiyomi.util.storage.getUriCompat import eu.kanade.tachiyomi.util.system.ImageUtil +import eu.kanade.tachiyomi.util.system.logcat +import logcat.LogPriority import okio.IOException import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream @@ -30,11 +32,7 @@ class ImageSaver( val type = ImageUtil.findImageType(data) ?: throw Exception("Not an image") val filename = DiskUtil.buildValidFilename("${image.name}.${type.extension}") - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { - return save(data(), image.location.directory(context), filename) - } - - if (image.location !is Location.Pictures) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || image.location !is Location.Pictures) { return save(data(), image.location.directory(context), filename) } @@ -54,13 +52,18 @@ class ImageSaver( val picture = context.contentResolver.insert( pictureDir, contentValues, - ) ?: throw IOException("Couldn't create file") + ) ?: throw IOException(context.getString(R.string.error_saving_picture)) - data().use { input -> - @Suppress("BlockingMethodInNonBlockingContext") - context.contentResolver.openOutputStream(picture, "w").use { output -> - input.copyTo(output!!) + try { + data().use { input -> + @Suppress("BlockingMethodInNonBlockingContext") + context.contentResolver.openOutputStream(picture, "w").use { output -> + input.copyTo(output!!) + } } + } catch (e: Exception) { + logcat(LogPriority.ERROR, e) + throw IOException(context.getString(R.string.error_saving_picture)) } DiskUtil.scanMedia(context, picture) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 882eddb27c..f7941e689c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -669,6 +669,7 @@ Picture saved + Error saving picture Custom filter