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 7a0e1f9ab..3496bebe6 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 @@ -33,7 +33,7 @@ class GroupEditDialog : BaseDialogFragment() { private val binding by viewBinding(DialogBookGroupEditBinding::bind) private val viewModel by viewModels() private var bookGroup: BookGroup? = null - val selectImage = registerForActivityResult(ActivityResultContractUtils.SelectImage()) { + val selectImage = registerForActivityResult(SelectImageContract()) { it?.second?.let { uri -> if (uri.isContentScheme()) { binding.ivCover.load(uri.toString()) 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 0f75f3536..8f983f1d0 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 @@ -22,7 +22,7 @@ class BookInfoEditActivity : VMBaseActivity(), ChangeCoverDialog.CallBack { - private val selectCover = registerForActivityResult(ActivityResultContractUtils.SelectImage()) { + private val selectCover = registerForActivityResult(SelectImageContract()) { it?.second?.let { uri -> coverChangeTo(uri) } 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 b9415734d..2fc6e3762 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 @@ -6,7 +6,6 @@ import android.graphics.Color import android.net.Uri import android.os.Bundle import android.view.* -import androidx.activity.result.contract.ActivityResultContracts import androidx.documentfile.provider.DocumentFile import com.jaredrummler.android.colorpicker.ColorPickerDialog import io.legado.app.R @@ -46,9 +45,10 @@ class BgTextConfigDialog : BaseDialogFragment() { private var primaryTextColor = 0 private var secondaryTextColor = 0 private val importFormNet = "网络导入" - private val selectBgImage = registerForActivityResult(ActivityResultContracts.GetContent()) { - it ?: return@registerForActivityResult - setBgFromUri(it) + private val selectBgImage = registerForActivityResult(SelectImageContract()) { + it?.second?.let { uri -> + setBgFromUri(uri) + } } private val selectExportDir = registerForActivityResult(FilePicker()) { it ?: return@registerForActivityResult @@ -122,7 +122,7 @@ class BgTextConfigDialog : BaseDialogFragment() { ivBg.setImageResource(R.drawable.ic_image) ivBg.setColorFilter(primaryTextColor) root.setOnClickListener { - selectBgImage.launch("image/*") + selectBgImage.launch(null) } } } 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 2367cc2cb..970c50b20 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 @@ -42,7 +42,7 @@ class ThemeConfigFragment : BasePreferenceFragment(), private val requestCodeCoverDark = 112 private val requestCodeBgLight = 121 private val requestCodeBgDark = 122 - private val selectImage = registerForActivityResult(ActivityResultContractUtils.SelectImage()) { + private val selectImage = registerForActivityResult(SelectImageContract()) { val uri = it?.second ?: return@registerForActivityResult when (it.first) { requestCodeCover -> setCoverFromUri(PreferKey.defaultCover, uri) diff --git a/app/src/main/java/io/legado/app/utils/ActivityResultContractUtils.kt b/app/src/main/java/io/legado/app/utils/ActivityResultContractUtils.kt deleted file mode 100644 index 66d15a888..000000000 --- a/app/src/main/java/io/legado/app/utils/ActivityResultContractUtils.kt +++ /dev/null @@ -1,32 +0,0 @@ -package io.legado.app.utils - -import android.app.Activity.RESULT_OK -import android.content.Context -import android.content.Intent -import android.net.Uri -import androidx.activity.result.contract.ActivityResultContract - -object ActivityResultContractUtils { - - class SelectImage : ActivityResultContract?>() { - - var requestCode: Int? = null - - override fun createIntent(context: Context, input: Int?): Intent { - requestCode = input - return Intent(Intent.ACTION_OPEN_DOCUMENT) - .addCategory(Intent.CATEGORY_OPENABLE) - .setType("image/*") - } - - override fun parseResult(resultCode: Int, intent: Intent?): Pair? { - if (resultCode == RESULT_OK) { - return Pair(requestCode, intent?.data) - } - return null - } - - } - - -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/utils/ActivityResultContracts.kt b/app/src/main/java/io/legado/app/utils/ActivityResultContracts.kt new file mode 100644 index 000000000..152912332 --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/ActivityResultContracts.kt @@ -0,0 +1,27 @@ +package io.legado.app.utils + +import android.app.Activity.RESULT_OK +import android.content.Context +import android.content.Intent +import android.net.Uri +import androidx.activity.result.contract.ActivityResultContract + +class SelectImageContract : ActivityResultContract?>() { + + var requestCode: Int? = null + + override fun createIntent(context: Context, input: Int?): Intent { + requestCode = input + return Intent(Intent.ACTION_OPEN_DOCUMENT) + .addCategory(Intent.CATEGORY_OPENABLE) + .setType("image/*") + } + + override fun parseResult(resultCode: Int, intent: Intent?): Pair? { + if (resultCode == RESULT_OK) { + return Pair(requestCode, intent?.data) + } + return null + } + +} \ No newline at end of file