Fix "Download split" not working while using SD card (#8305)

* Fix "Download split" not working while using SD card

* Update app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt

Co-authored-by: arkon <arkon@users.noreply.github.com>
This commit is contained in:
AntsyLich 2022-10-28 21:40:43 +06:00 committed by GitHub
parent ebddb96373
commit 6e4a30e593
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 14 deletions

View File

@ -485,17 +485,15 @@ class Downloader(
private fun splitTallImageIfNeeded(page: Page, tmpDir: UniFile): Boolean { private fun splitTallImageIfNeeded(page: Page, tmpDir: UniFile): Boolean {
if (!downloadPreferences.splitTallImages().get()) return true if (!downloadPreferences.splitTallImages().get()) return true
val filename = String.format("%03d", page.number) val filenamePrefix = String.format("%03d", page.number)
val imageFile = tmpDir.listFiles()?.find { it.name!!.startsWith(filename) } val imageFile = tmpDir.listFiles()?.firstOrNull { it.name.orEmpty().startsWith(filenamePrefix) }
?: throw Error(context.getString(R.string.download_notifier_split_page_not_found, page.number)) ?: throw Error(context.getString(R.string.download_notifier_split_page_not_found, page.number))
val imageFilePath = imageFile.filePath
?: throw Error(context.getString(R.string.download_notifier_split_page_path_not_found, page.number))
// check if the original page was previously splitted before then skip. // check if the original page was previously splitted before then skip.
if (imageFile.name!!.contains("__")) return true if (imageFile.name!!.contains("__")) return true
return try { return try {
ImageUtil.splitTallImage(imageFile, imageFilePath) ImageUtil.splitTallImage(tmpDir, imageFile, filenamePrefix)
} catch (e: Exception) { } catch (e: Exception) {
logcat(LogPriority.ERROR, e) logcat(LogPriority.ERROR, e)
false false

View File

@ -27,8 +27,6 @@ import tachiyomi.decoder.ImageDecoder
import java.io.BufferedInputStream import java.io.BufferedInputStream
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream import java.io.InputStream
import java.net.URLConnection import java.net.URLConnection
import kotlin.math.abs import kotlin.math.abs
@ -202,7 +200,7 @@ object ImageUtil {
/** /**
* Splits tall images to improve performance of reader * Splits tall images to improve performance of reader
*/ */
fun splitTallImage(imageFile: UniFile, imageFilePath: String): Boolean { fun splitTallImage(tmpDir: UniFile, imageFile: UniFile, filenamePrefix: String): Boolean {
if (isAnimatedAndSupported(imageFile.openInputStream()) || !isTallImage(imageFile.openInputStream())) { if (isAnimatedAndSupported(imageFile.openInputStream()) || !isTallImage(imageFile.openInputStream())) {
return true return true
} }
@ -221,11 +219,15 @@ object ImageUtil {
return try { return try {
splitDataList.forEach { splitData -> splitDataList.forEach { splitData ->
val splitPath = splitImagePath(imageFilePath, splitData.index) val splitImageName = splitImageName(filenamePrefix, splitData.index)
// Remove pre-existing split if exists (this split shouldn't exist under normal circumstances)
tmpDir.findFile(splitImageName)?.delete()
val splitFile = tmpDir.createFile(splitImageName)
val region = Rect(0, splitData.topOffset, splitData.splitWidth, splitData.bottomOffset) val region = Rect(0, splitData.topOffset, splitData.splitWidth, splitData.bottomOffset)
FileOutputStream(splitPath).use { outputStream -> splitFile.openOutputStream().use { outputStream ->
val splitBitmap = bitmapRegionDecoder.decodeRegion(region, options) val splitBitmap = bitmapRegionDecoder.decodeRegion(region, options)
splitBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream) splitBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
splitBitmap.recycle() splitBitmap.recycle()
@ -240,8 +242,8 @@ object ImageUtil {
} catch (e: Exception) { } catch (e: Exception) {
// Image splits were not successfully saved so delete them and keep the original image // Image splits were not successfully saved so delete them and keep the original image
splitDataList splitDataList
.map { splitImagePath(imageFilePath, it.index) } .map { splitImageName(filenamePrefix, it.index) }
.forEach { File(it).delete() } .forEach { tmpDir.findFile(it)?.delete() }
logcat(LogPriority.ERROR, e) logcat(LogPriority.ERROR, e)
false false
} finally { } finally {
@ -249,8 +251,7 @@ object ImageUtil {
} }
} }
private fun splitImagePath(imageFilePath: String, index: Int) = private fun splitImageName(filenamePrefix: String, index: Int) = "${filenamePrefix}__${"%03d".format(index + 1)}.jpg"
imageFilePath.substringBeforeLast(".") + "__${"%03d".format(index + 1)}.jpg"
/** /**
* Check whether the image is a long Strip that needs splitting * Check whether the image is a long Strip that needs splitting