pull/69/head
kunfei 5 years ago
parent 33a1a5ccef
commit c8f2ece30f
  1. 41
      app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt
  2. 12
      app/src/main/java/io/legado/app/utils/DocumentUtils.kt

@ -12,6 +12,7 @@ import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.documentfile.provider.DocumentFile
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
@ -20,11 +21,13 @@ import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.constant.Bus
import io.legado.app.help.FileHelp
import io.legado.app.help.ImageLoader
import io.legado.app.help.ReadBookConfig
import io.legado.app.help.permission.Permissions
import io.legado.app.help.permission.PermissionsCompat
import io.legado.app.ui.book.read.Help
import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.FileUtils
import io.legado.app.utils.getCompatColor
import io.legado.app.utils.postEvent
@ -32,6 +35,7 @@ import kotlinx.android.synthetic.main.dialog_read_bg_text.*
import kotlinx.android.synthetic.main.item_bg_image.view.*
import org.jetbrains.anko.sdk27.listeners.onCheckedChange
import org.jetbrains.anko.sdk27.listeners.onClick
import java.io.File
class BgTextConfigDialog : DialogFragment() {
@ -133,17 +137,10 @@ class BgTextConfigDialog : DialogFragment() {
}
private fun selectImage() {
PermissionsCompat.Builder(this)
.addPermissions(Permissions.READ_EXTERNAL_STORAGE, Permissions.WRITE_EXTERNAL_STORAGE)
.rationale(R.string.bg_image_per)
.onGranted {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/*"
startActivityForResult(intent, resultSelectBg)
Unit
}
.request()
}
class BgAdapter(context: Context) :
@ -170,12 +167,38 @@ class BgTextConfigDialog : DialogFragment() {
when (requestCode) {
resultSelectBg -> {
if (resultCode == RESULT_OK) {
data?.data?.let {
FileUtils.getPath(requireContext(), it)?.let { path ->
data?.data?.let { uri ->
if (DocumentFile.isDocumentUri(requireContext(), uri)) {
val doc = DocumentFile.fromSingleUri(requireContext(), uri)
doc?.let {
var file = requireContext().getExternalFilesDir(null)
?: requireContext().filesDir
file =
FileHelp.getFile(file.absolutePath + File.separator + "bg" + File.separator + doc.name)
DocumentUtils.readBytes(requireContext(), uri)?.let {
file.writeBytes(it)
ReadBookConfig.getConfig().setBg(2, file.absolutePath)
ReadBookConfig.upBg()
postEvent(Bus.UP_CONFIG, false)
}
}
} else {
PermissionsCompat.Builder(this)
.addPermissions(
Permissions.READ_EXTERNAL_STORAGE,
Permissions.WRITE_EXTERNAL_STORAGE
)
.rationale(R.string.bg_image_per)
.onGranted {
FileUtils.getPath(requireContext(), uri)?.let { path ->
ReadBookConfig.getConfig().setBg(2, path)
ReadBookConfig.upBg()
postEvent(Bus.UP_CONFIG, false)
}
Unit
}
.request()
}
}
}
}

@ -6,25 +6,24 @@ import android.net.Uri
object DocumentUtils {
@JvmStatic
@Throws(Exception::class)
fun writeText(context: Context, data: String, fileUri: Uri): Boolean {
return writeBytes(context, data.toByteArray(), fileUri)
}
@JvmStatic
@Throws(Exception::class)
fun writeBytes(context: Context, data: ByteArray, fileUri: Uri): Boolean {
try {
context.contentResolver.openOutputStream(fileUri)?.let {
it.write(data)
it.close()
return true
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return false
}
@JvmStatic
@Throws(Exception::class)
fun readText(context: Context, uri: Uri): String? {
readBytes(context, uri)?.let {
return String(it)
@ -33,8 +32,8 @@ object DocumentUtils {
}
@JvmStatic
@Throws(Exception::class)
fun readBytes(context: Context, uri: Uri): ByteArray? {
try {
context.contentResolver.openInputStream(uri)?.let {
val len: Int = it.available()
val buffer = ByteArray(len)
@ -42,9 +41,6 @@ object DocumentUtils {
it.close()
return buffer
}
} catch (e: Exception) {
e.printStackTrace()
}
return null
}

Loading…
Cancel
Save