From a8526dd47b05d6eb794b5a91fda425d235ca689d Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 21 Aug 2021 18:12: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 | 2 +- .../ui/book/info/edit/BookInfoEditActivity.kt | 4 +-- .../ui/book/read/config/BgTextConfigDialog.kt | 2 +- .../app/ui/config/ThemeConfigFragment.kt | 4 +-- .../java/io/legado/app/utils/UriExtensions.kt | 30 ++++++++++--------- 5 files changed, 22 insertions(+), 20 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 8237b7830..4994df880 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,7 +34,7 @@ class GroupEditDialog : BaseDialogFragment() { private val viewModel by viewModels() private var bookGroup: BookGroup? = null val selectImage = registerForActivityResult(SelectImageContract()) { - it?.second?.read(this) { name, bytes -> + readUri(it?.second) { name, bytes -> var file = requireContext().externalFiles file = FileUtils.createFileIfNotExist(file, "covers", name) file.writeBytes(bytes) 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 88a76ae4b..8d6ca1099 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 @@ -14,7 +14,7 @@ import io.legado.app.ui.book.changecover.ChangeCoverDialog 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.readUri import io.legado.app.utils.viewbindingdelegate.viewBinding class BookInfoEditActivity : @@ -102,7 +102,7 @@ class BookInfoEditActivity : } private fun coverChangeTo(uri: Uri) { - uri.read(this) { name, bytes -> + readUri(uri) { name, bytes -> var file = this.externalFiles file = FileUtils.createFileIfNotExist(file, "covers", name) file.writeBytes(bytes) diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index dd2499628..b905b1e74 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -343,7 +343,7 @@ class BgTextConfigDialog : BaseDialogFragment() { } private fun setBgFromUri(uri: Uri) { - uri.read(this) { name, bytes -> + readUri(uri) { name, bytes -> var file = requireContext().externalFiles file = FileUtils.createFileIfNotExist(file, "bg", name) file.writeBytes(bytes) diff --git a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt index 1ef0dcbbf..7712f61b3 100644 --- a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt @@ -302,7 +302,7 @@ class ThemeConfigFragment : BasePreferenceFragment(), } private fun setBgFromUri(uri: Uri, preferenceKey: String, success: () -> Unit) { - uri.read(this) { name, bytes -> + readUri(uri) { name, bytes -> var file = requireContext().externalFiles file = FileUtils.createFileIfNotExist(file, preferenceKey, name) file.writeBytes(bytes) @@ -312,7 +312,7 @@ class ThemeConfigFragment : BasePreferenceFragment(), } private fun setCoverFromUri(preferenceKey: String, uri: Uri) { - uri.read(this) { name, bytes -> + readUri(uri) { name, bytes -> var file = requireContext().externalFiles file = FileUtils.createFileIfNotExist(file, "covers", name) file.writeBytes(bytes) 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 c461f8555..5d52b26dd 100644 --- a/app/src/main/java/io/legado/app/utils/UriExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/UriExtensions.kt @@ -15,24 +15,25 @@ fun Uri.isContentScheme() = this.scheme == "content" /** * 读取URI */ -fun Uri.read(activity: AppCompatActivity, success: (name: String, bytes: ByteArray) -> Unit) { +fun AppCompatActivity.readUri(uri: Uri?, success: (name: String, bytes: ByteArray) -> Unit) { + uri ?: return try { - if (isContentScheme()) { - val doc = DocumentFile.fromSingleUri(activity, this) + if (uri.isContentScheme()) { + val doc = DocumentFile.fromSingleUri(this, uri) doc ?: error("未获取到文件") val name = doc.name ?: error("未获取到文件名") - val fileBytes = DocumentUtils.readBytes(activity, doc.uri) + val fileBytes = DocumentUtils.readBytes(this, doc.uri) fileBytes ?: error("读取文件出错") success.invoke(name, fileBytes) } else { - PermissionsCompat.Builder(activity) + PermissionsCompat.Builder(this) .addPermissions( Permissions.READ_EXTERNAL_STORAGE, Permissions.WRITE_EXTERNAL_STORAGE ) .rationale(R.string.bg_image_per) .onGranted { - RealPathUtil.getPath(activity, this)?.let { path -> + RealPathUtil.getPath(this, uri)?.let { path -> val imgFile = File(path) success.invoke(imgFile.name, imgFile.readBytes()) } @@ -41,31 +42,32 @@ fun Uri.read(activity: AppCompatActivity, success: (name: String, bytes: ByteArr } } catch (e: Exception) { e.printStackTrace() - activity.toastOnUi(e.localizedMessage ?: "read uri error") + toastOnUi(e.localizedMessage ?: "read uri error") } } /** * 读取URI */ -fun Uri.read(fragment: Fragment, success: (name: String, bytes: ByteArray) -> Unit) { +fun Fragment.readUri(uri: Uri?, success: (name: String, bytes: ByteArray) -> Unit) { + uri ?: return try { - if (isContentScheme()) { - val doc = DocumentFile.fromSingleUri(fragment.requireContext(), this) + if (uri.isContentScheme()) { + val doc = DocumentFile.fromSingleUri(requireContext(), uri) doc ?: error("未获取到文件") val name = doc.name ?: error("未获取到文件名") - val fileBytes = DocumentUtils.readBytes(fragment.requireContext(), doc.uri) + val fileBytes = DocumentUtils.readBytes(requireContext(), doc.uri) fileBytes ?: error("读取文件出错") success.invoke(name, fileBytes) } else { - PermissionsCompat.Builder(fragment) + PermissionsCompat.Builder(this) .addPermissions( Permissions.READ_EXTERNAL_STORAGE, Permissions.WRITE_EXTERNAL_STORAGE ) .rationale(R.string.bg_image_per) .onGranted { - RealPathUtil.getPath(fragment.requireContext(), this)?.let { path -> + RealPathUtil.getPath(requireContext(), uri)?.let { path -> val imgFile = File(path) success.invoke(imgFile.name, imgFile.readBytes()) } @@ -74,7 +76,7 @@ fun Uri.read(fragment: Fragment, success: (name: String, bytes: ByteArray) -> Un } } catch (e: Exception) { e.printStackTrace() - fragment.toastOnUi(e.localizedMessage ?: "read uri error") + toastOnUi(e.localizedMessage ?: "read uri error") } }