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 82e7113a0..b19b86c91 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 @@ -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() + val intent = Intent(Intent.ACTION_GET_CONTENT) + intent.addCategory(Intent.CATEGORY_OPENABLE) + intent.type = "image/*" + startActivityForResult(intent, resultSelectBg) } class BgAdapter(context: Context) : @@ -170,11 +167,37 @@ class BgTextConfigDialog : DialogFragment() { when (requestCode) { resultSelectBg -> { if (resultCode == RESULT_OK) { - data?.data?.let { - FileUtils.getPath(requireContext(), it)?.let { path -> - ReadBookConfig.getConfig().setBg(2, path) - ReadBookConfig.upBg() - postEvent(Bus.UP_CONFIG, false) + 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() } } } diff --git a/app/src/main/java/io/legado/app/utils/DocumentUtils.kt b/app/src/main/java/io/legado/app/utils/DocumentUtils.kt index dcb128a8e..3929874ef 100644 --- a/app/src/main/java/io/legado/app/utils/DocumentUtils.kt +++ b/app/src/main/java/io/legado/app/utils/DocumentUtils.kt @@ -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() + context.contentResolver.openOutputStream(fileUri)?.let { + it.write(data) + it.close() + return true } return false } @JvmStatic + @Throws(Exception::class) fun readText(context: Context, uri: Uri): String? { readBytes(context, uri)?.let { return String(it) @@ -33,17 +32,14 @@ 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) - it.read(buffer) - it.close() - return buffer - } - } catch (e: Exception) { - e.printStackTrace() + context.contentResolver.openInputStream(uri)?.let { + val len: Int = it.available() + val buffer = ByteArray(len) + it.read(buffer) + it.close() + return buffer } return null }