From 009555b2f8c9c86e7b295bc487aae543ecd8c19f Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 19 Oct 2021 10:50:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../book/read/config/SpeakEngineViewModel.kt | 6 ++---- .../book/source/manage/BookSourceActivity.kt | 4 +--- .../java/io/legado/app/utils/DocumentUtils.kt | 17 +++++++---------- .../java/io/legado/app/utils/UriExtensions.kt | 18 ++++++++---------- 4 files changed, 18 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt index f8033f57f..d12f2e76c 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt @@ -42,13 +42,11 @@ class SpeakEngineViewModel(application: Application) : BaseViewModel(application fun importLocal(uri: Uri) { execute { - uri.readText(context)?.let { - import(it) - } + import(uri.readText(context)) }.onSuccess { context.toastOnUi("导入成功") }.onError { - context.toastOnUi("导入失败") + context.toastOnUi("导入失败\n${it.localizedMessage}") } } diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index c896580ae..38f194b0f 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -67,9 +67,7 @@ class BookSourceActivity : VMBaseActivity uri ?: return@registerForActivityResult try { - uri.readText(this)?.let { - showDialogFragment(ImportBookSourceDialog(it)) - } + showDialogFragment(ImportBookSourceDialog(uri.readText(this))) } catch (e: Exception) { toastOnUi("readTextError:${e.localizedMessage}") } diff --git a/app/src/main/java/io/legado/app/utils/DocumentUtils.kt b/app/src/main/java/io/legado/app/utils/DocumentUtils.kt index 40274b8a3..64fa913eb 100644 --- a/app/src/main/java/io/legado/app/utils/DocumentUtils.kt +++ b/app/src/main/java/io/legado/app/utils/DocumentUtils.kt @@ -5,6 +5,7 @@ import android.database.Cursor import android.net.Uri import android.provider.DocumentsContract import androidx.documentfile.provider.DocumentFile +import io.legado.app.model.NoStackTraceException import timber.log.Timber import java.nio.charset.Charset import java.util.* @@ -76,24 +77,20 @@ object DocumentUtils { @JvmStatic @Throws(Exception::class) - fun readText(context: Context, uri: Uri): String? { - readBytes(context, uri)?.let { - return String(it) - } - return null + fun readText(context: Context, uri: Uri): String { + return String(readBytes(context, uri)) } @JvmStatic @Throws(Exception::class) - fun readBytes(context: Context, uri: Uri): ByteArray? { + fun readBytes(context: Context, uri: Uri): ByteArray { context.contentResolver.openInputStream(uri)?.let { val len: Int = it.available() val buffer = ByteArray(len) it.read(buffer) it.close() return buffer - } - return null + } ?: throw NoStackTraceException("打开文件失败\n${uri}") } fun listFiles(context: Context, uri: Uri): ArrayList { @@ -166,11 +163,11 @@ fun DocumentFile.writeBytes(context: Context, data: ByteArray) { } @Throws(Exception::class) -fun DocumentFile.readText(context: Context): String? { +fun DocumentFile.readText(context: Context): String { return DocumentUtils.readText(context, this.uri) } @Throws(Exception::class) -fun DocumentFile.readBytes(context: Context): ByteArray? { +fun DocumentFile.readBytes(context: Context): ByteArray { return DocumentUtils.readBytes(context, this.uri) } diff --git a/app/src/main/java/io/legado/app/utils/UriExtensions.kt b/app/src/main/java/io/legado/app/utils/UriExtensions.kt index 55fe96450..70717fe3b 100644 --- a/app/src/main/java/io/legado/app/utils/UriExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/UriExtensions.kt @@ -25,7 +25,6 @@ fun AppCompatActivity.readUri(uri: Uri?, success: (name: String, bytes: ByteArra doc ?: throw NoStackTraceException("未获取到文件") val name = doc.name ?: throw NoStackTraceException("未获取到文件名") val fileBytes = DocumentUtils.readBytes(this, doc.uri) - fileBytes ?: throw NoStackTraceException("读取文件出错") success.invoke(name, fileBytes) } else { PermissionsCompat.Builder(this) @@ -59,7 +58,6 @@ fun Fragment.readUri(uri: Uri?, success: (name: String, bytes: ByteArray) -> Uni doc ?: throw NoStackTraceException("未获取到文件") val name = doc.name ?: throw NoStackTraceException("未获取到文件名") val fileBytes = DocumentUtils.readBytes(requireContext(), doc.uri) - fileBytes ?: throw NoStackTraceException("读取文件出错") success.invoke(name, fileBytes) } else { PermissionsCompat.Builder(this) @@ -83,24 +81,24 @@ fun Fragment.readUri(uri: Uri?, success: (name: String, bytes: ByteArray) -> Uni } @Throws(Exception::class) -fun Uri.readBytes(context: Context): ByteArray? { - if (this.isContentScheme()) { - return DocumentUtils.readBytes(context, this) +fun Uri.readBytes(context: Context): ByteArray { + return if (this.isContentScheme()) { + DocumentUtils.readBytes(context, this) } else { val path = RealPathUtil.getPath(context, this) if (path?.isNotEmpty() == true) { - return File(path).readBytes() + File(path).readBytes() + } else { + throw NoStackTraceException("获取文件真实地址失败\n${this.path}") } } - return null } @Throws(Exception::class) -fun Uri.readText(context: Context): String? { - readBytes(context)?.let { +fun Uri.readText(context: Context): String { + readBytes(context).let { return String(it) } - return null } @Throws(Exception::class)