pull/1259/head
gedoor 3 years ago
parent c8d12cb8e1
commit a8526dd47b
  1. 2
      app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt
  2. 4
      app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt
  3. 2
      app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt
  4. 4
      app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt
  5. 30
      app/src/main/java/io/legado/app/utils/UriExtensions.kt

@ -34,7 +34,7 @@ class GroupEditDialog : BaseDialogFragment() {
private val viewModel by viewModels<GroupViewModel>()
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)

@ -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)

@ -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)

@ -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)

@ -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")
}
}

Loading…
Cancel
Save