浏览代码

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>
AntsyLich 2 年之前
父节点
当前提交
6e4a30e593

+ 3 - 5
app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt

@@ -485,17 +485,15 @@ class Downloader(
     private fun splitTallImageIfNeeded(page: Page, tmpDir: UniFile): Boolean {
         if (!downloadPreferences.splitTallImages().get()) return true
 
-        val filename = String.format("%03d", page.number)
-        val imageFile = tmpDir.listFiles()?.find { it.name!!.startsWith(filename) }
+        val filenamePrefix = String.format("%03d", page.number)
+        val imageFile = tmpDir.listFiles()?.firstOrNull { it.name.orEmpty().startsWith(filenamePrefix) }
             ?: 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.
         if (imageFile.name!!.contains("__")) return true
 
         return try {
-            ImageUtil.splitTallImage(imageFile, imageFilePath)
+            ImageUtil.splitTallImage(tmpDir, imageFile, filenamePrefix)
         } catch (e: Exception) {
             logcat(LogPriority.ERROR, e)
             false

+ 10 - 9
app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt

@@ -27,8 +27,6 @@ import tachiyomi.decoder.ImageDecoder
 import java.io.BufferedInputStream
 import java.io.ByteArrayInputStream
 import java.io.ByteArrayOutputStream
-import java.io.File
-import java.io.FileOutputStream
 import java.io.InputStream
 import java.net.URLConnection
 import kotlin.math.abs
@@ -202,7 +200,7 @@ object ImageUtil {
     /**
      * 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())) {
             return true
         }
@@ -221,11 +219,15 @@ object ImageUtil {
 
         return try {
             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)
 
-                FileOutputStream(splitPath).use { outputStream ->
+                splitFile.openOutputStream().use { outputStream ->
                     val splitBitmap = bitmapRegionDecoder.decodeRegion(region, options)
                     splitBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
                     splitBitmap.recycle()
@@ -240,8 +242,8 @@ object ImageUtil {
         } catch (e: Exception) {
             // Image splits were not successfully saved so delete them and keep the original image
             splitDataList
-                .map { splitImagePath(imageFilePath, it.index) }
-                .forEach { File(it).delete() }
+                .map { splitImageName(filenamePrefix, it.index) }
+                .forEach { tmpDir.findFile(it)?.delete() }
             logcat(LogPriority.ERROR, e)
             false
         } finally {
@@ -249,8 +251,7 @@ object ImageUtil {
         }
     }
 
-    private fun splitImagePath(imageFilePath: String, index: Int) =
-        imageFilePath.substringBeforeLast(".") + "__${"%03d".format(index + 1)}.jpg"
+    private fun splitImageName(filenamePrefix: String, index: Int) = "${filenamePrefix}__${"%03d".format(index + 1)}.jpg"
 
     /**
      * Check whether the image is a long Strip that needs splitting