From c43dcd121628d05e3ee64ce5d459c53657551f81 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 12 Aug 2021 22:56:42 +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 --- .../app/ui/book/group/GroupEditDialog.kt | 11 ++-- .../ui/book/info/edit/BookInfoEditActivity.kt | 45 +++------------ .../java/io/legado/app/utils/UriExtensions.kt | 57 +++++++++++++++++++ 3 files changed, 71 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt index 3496bebe6..8237b7830 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt @@ -34,12 +34,11 @@ class GroupEditDialog : BaseDialogFragment() { private val viewModel by viewModels() private var bookGroup: BookGroup? = null val selectImage = registerForActivityResult(SelectImageContract()) { - it?.second?.let { uri -> - if (uri.isContentScheme()) { - binding.ivCover.load(uri.toString()) - } else { - binding.ivCover.load(uri.path) - } + it?.second?.read(this) { name, bytes -> + var file = requireContext().externalFiles + file = FileUtils.createFileIfNotExist(file, "covers", name) + file.writeBytes(bytes) + binding.ivCover.load(file.absolutePath) } } diff --git a/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt index 8f983f1d0..88a76ae4b 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt @@ -6,17 +6,16 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import androidx.activity.viewModels -import androidx.documentfile.provider.DocumentFile import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.data.entities.Book import io.legado.app.databinding.ActivityBookInfoEditBinding -import io.legado.app.lib.permission.Permissions -import io.legado.app.lib.permission.PermissionsCompat import io.legado.app.ui.book.changecover.ChangeCoverDialog -import io.legado.app.utils.* +import io.legado.app.utils.FileUtils +import io.legado.app.utils.SelectImageContract +import io.legado.app.utils.externalFiles +import io.legado.app.utils.read import io.legado.app.utils.viewbindingdelegate.viewBinding -import java.io.File class BookInfoEditActivity : VMBaseActivity(), @@ -103,37 +102,11 @@ class BookInfoEditActivity : } private fun coverChangeTo(uri: Uri) { - if (uri.isContentScheme()) { - val doc = DocumentFile.fromSingleUri(this, uri) - doc?.name?.let { - var file = this.externalFiles - file = FileUtils.createFileIfNotExist(file, "covers", it) - kotlin.runCatching { - DocumentUtils.readBytes(this, doc.uri) - }.getOrNull()?.let { byteArray -> - file.writeBytes(byteArray) - coverChangeTo(file.absolutePath) - } ?: toastOnUi("获取文件出错") - } - } else { - PermissionsCompat.Builder(this) - .addPermissions( - Permissions.READ_EXTERNAL_STORAGE, - Permissions.WRITE_EXTERNAL_STORAGE - ) - .rationale(R.string.bg_image_per) - .onGranted { - RealPathUtil.getPath(this, uri)?.let { path -> - val imgFile = File(path) - if (imgFile.exists()) { - var file = this.externalFiles - file = FileUtils.createFileIfNotExist(file, "covers", imgFile.name) - file.writeBytes(imgFile.readBytes()) - coverChangeTo(file.absolutePath) - } - } - } - .request() + uri.read(this) { name, bytes -> + var file = this.externalFiles + file = FileUtils.createFileIfNotExist(file, "covers", name) + file.writeBytes(bytes) + coverChangeTo(file.absolutePath) } } 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 3e99bd0a2..5e9c1a2df 100644 --- a/app/src/main/java/io/legado/app/utils/UriExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/UriExtensions.kt @@ -2,11 +2,68 @@ package io.legado.app.utils import android.content.Context import android.net.Uri +import androidx.appcompat.app.AppCompatActivity import androidx.documentfile.provider.DocumentFile +import androidx.fragment.app.Fragment +import io.legado.app.R +import io.legado.app.lib.permission.Permissions +import io.legado.app.lib.permission.PermissionsCompat import java.io.File fun Uri.isContentScheme() = this.scheme == "content" +@Throws(Exception::class) +fun Uri.read(activity: AppCompatActivity, success: (name: String, bytes: ByteArray) -> Unit) { + if (isContentScheme()) { + val doc = DocumentFile.fromSingleUri(activity, this) + doc ?: error("未获取到文件") + val name = doc.name ?: error("未获取到文件名") + val fileBytes = DocumentUtils.readBytes(activity, doc.uri) + fileBytes ?: error("读取文件出错") + success.invoke(name, fileBytes) + } else { + PermissionsCompat.Builder(activity) + .addPermissions( + Permissions.READ_EXTERNAL_STORAGE, + Permissions.WRITE_EXTERNAL_STORAGE + ) + .rationale(R.string.bg_image_per) + .onGranted { + RealPathUtil.getPath(activity, this)?.let { path -> + val imgFile = File(path) + success.invoke(imgFile.name, imgFile.readBytes()) + } + } + .request() + } +} + +@Throws(Exception::class) +fun Uri.read(fragment: Fragment, success: (name: String, bytes: ByteArray) -> Unit) { + if (isContentScheme()) { + val doc = DocumentFile.fromSingleUri(fragment.requireContext(), this) + doc ?: error("未获取到文件") + val name = doc.name ?: error("未获取到文件名") + val fileBytes = DocumentUtils.readBytes(fragment.requireContext(), doc.uri) + fileBytes ?: error("读取文件出错") + success.invoke(name, fileBytes) + } else { + PermissionsCompat.Builder(fragment) + .addPermissions( + Permissions.READ_EXTERNAL_STORAGE, + Permissions.WRITE_EXTERNAL_STORAGE + ) + .rationale(R.string.bg_image_per) + .onGranted { + RealPathUtil.getPath(fragment.requireContext(), this)?.let { path -> + val imgFile = File(path) + success.invoke(imgFile.name, imgFile.readBytes()) + } + } + .request() + } +} + @Throws(Exception::class) fun Uri.readBytes(context: Context): ByteArray? { if (this.isContentScheme()) {