Skip to content

Commit

Permalink
Avoid crashing if picture can't be saved (related to #6905)
Browse files Browse the repository at this point in the history
  • Loading branch information
arkon committed Apr 13, 2022
1 parent d443245 commit 5da2c82
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
23 changes: 13 additions & 10 deletions app/src/main/java/eu/kanade/tachiyomi/data/saver/ImageSaver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@

<!-- Image notifier -->
<string name="picture_saved">Picture saved</string>
<string name="error_saving_picture">Error saving picture</string>

<!-- Reader activity -->
<string name="custom_filter">Custom filter</string>
Expand Down

0 comments on commit 5da2c82

Please sign in to comment.