mirror of
https://github.com/gedoor/legado.git
synced 2024-09-01 09:34:25 +08:00
优化
This commit is contained in:
parent
2d0d7447ef
commit
009555b2f8
@ -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}")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -67,9 +67,7 @@ class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceV
|
||||
private val importDoc = registerForActivityResult(HandleFileContract()) { uri ->
|
||||
uri ?: return@registerForActivityResult
|
||||
try {
|
||||
uri.readText(this)?.let {
|
||||
showDialogFragment(ImportBookSourceDialog(it))
|
||||
}
|
||||
showDialogFragment(ImportBookSourceDialog(uri.readText(this)))
|
||||
} catch (e: Exception) {
|
||||
toastOnUi("readTextError:${e.localizedMessage}")
|
||||
}
|
||||
|
@ -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<DocItem> {
|
||||
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user