From 44f0a217bc6f2f2cf838bb9e89cca5d3b1de9a24 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 30 Dec 2021 22:12:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E4=B9=A6=E7=B1=8D=E6=97=A0?= =?UTF-8?q?=E6=9D=83=E9=99=90=E5=88=99=E4=BF=9D=E5=AD=98=E5=88=B0=E8=87=AA?= =?UTF-8?q?=E5=B7=B1=E9=80=89=E5=AE=9A=E7=9A=84=E6=96=87=E4=BB=B6=E5=A4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/association/FileAssociationActivity.kt | 47 ++++++++++++++----- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt b/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt index 9764e5c42..673e174e0 100644 --- a/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt +++ b/app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt @@ -18,6 +18,8 @@ import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import java.io.File +import java.io.FileOutputStream class FileAssociationActivity : VMBaseActivity() { @@ -91,24 +93,43 @@ class FileAssociationActivity : } private fun importBook(treeUri: Uri, uri: Uri) { - val treeDoc = DocumentFile.fromTreeUri(this, treeUri) - val bookDoc = DocumentFile.fromSingleUri(this, uri) launch { runCatching { - withContext(IO) { - val name = bookDoc?.name!! - val doc = treeDoc!!.findFile(name) - if (doc != null) { - viewModel.importBook(doc.uri) - } else { - val nDoc = treeDoc.createFile(FileUtils.getMimeType(name), name)!! - contentResolver.openOutputStream(nDoc.uri)!!.use { oStream -> - contentResolver.openInputStream(bookDoc.uri)!!.use { iStream -> - iStream.copyTo(oStream) - oStream.flush() + if (treeUri.isContentScheme()) { + val treeDoc = DocumentFile.fromTreeUri(this@FileAssociationActivity, treeUri) + val bookDoc = DocumentFile.fromSingleUri(this@FileAssociationActivity, uri) + withContext(IO) { + val name = bookDoc?.name!! + val doc = treeDoc!!.findFile(name) + if (doc != null) { + viewModel.importBook(doc.uri) + } else { + val nDoc = treeDoc.createFile(FileUtils.getMimeType(name), name)!! + contentResolver.openOutputStream(nDoc.uri)!!.use { oStream -> + contentResolver.openInputStream(bookDoc.uri)!!.use { iStream -> + iStream.copyTo(oStream) + oStream.flush() + } } + viewModel.importBook(nDoc.uri) } } + } else { + val treeFile = File(treeUri.path!!) + val bookDoc = DocumentFile.fromSingleUri(this@FileAssociationActivity, uri) + withContext(IO) { + val name = bookDoc?.name!! + val file = treeFile.getFile(name) + if (!file.exists() || file.lastModified() < bookDoc.lastModified()) { + FileOutputStream(file).use { oStream -> + contentResolver.openInputStream(bookDoc.uri)!!.use { iStream -> + iStream.copyTo(oStream) + oStream.flush() + } + } + } + viewModel.importBook(Uri.fromFile(file)) + } } }.onFailure { toastOnUi(it.localizedMessage)