pull/1238/head^2
gedoor 3 years ago
parent e3f93e0407
commit c43dcd1216
  1. 11
      app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt
  2. 45
      app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt
  3. 57
      app/src/main/java/io/legado/app/utils/UriExtensions.kt

@ -34,12 +34,11 @@ class GroupEditDialog : BaseDialogFragment() {
private val viewModel by viewModels<GroupViewModel>() private val viewModel by viewModels<GroupViewModel>()
private var bookGroup: BookGroup? = null private var bookGroup: BookGroup? = null
val selectImage = registerForActivityResult(SelectImageContract()) { val selectImage = registerForActivityResult(SelectImageContract()) {
it?.second?.let { uri -> it?.second?.read(this) { name, bytes ->
if (uri.isContentScheme()) { var file = requireContext().externalFiles
binding.ivCover.load(uri.toString()) file = FileUtils.createFileIfNotExist(file, "covers", name)
} else { file.writeBytes(bytes)
binding.ivCover.load(uri.path) binding.ivCover.load(file.absolutePath)
}
} }
} }

@ -6,17 +6,16 @@ import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.documentfile.provider.DocumentFile
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.databinding.ActivityBookInfoEditBinding 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.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 io.legado.app.utils.viewbindingdelegate.viewBinding
import java.io.File
class BookInfoEditActivity : class BookInfoEditActivity :
VMBaseActivity<ActivityBookInfoEditBinding, BookInfoEditViewModel>(), VMBaseActivity<ActivityBookInfoEditBinding, BookInfoEditViewModel>(),
@ -103,37 +102,11 @@ class BookInfoEditActivity :
} }
private fun coverChangeTo(uri: Uri) { private fun coverChangeTo(uri: Uri) {
if (uri.isContentScheme()) { uri.read(this) { name, bytes ->
val doc = DocumentFile.fromSingleUri(this, uri) var file = this.externalFiles
doc?.name?.let { file = FileUtils.createFileIfNotExist(file, "covers", name)
var file = this.externalFiles file.writeBytes(bytes)
file = FileUtils.createFileIfNotExist(file, "covers", it) coverChangeTo(file.absolutePath)
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()
} }
} }

@ -2,11 +2,68 @@ package io.legado.app.utils
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import androidx.documentfile.provider.DocumentFile 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 import java.io.File
fun Uri.isContentScheme() = this.scheme == "content" 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) @Throws(Exception::class)
fun Uri.readBytes(context: Context): ByteArray? { fun Uri.readBytes(context: Context): ByteArray? {
if (this.isContentScheme()) { if (this.isContentScheme()) {

Loading…
Cancel
Save