pull/1295/head
gedoor 3 years ago
parent ad828eb4aa
commit 14bf8960ff
  1. 26
      app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt
  2. 28
      app/src/main/java/io/legado/app/ui/document/HandleFileViewModel.kt

@ -31,17 +31,17 @@ class HandleFileActivity :
private var mode = 0 private var mode = 0
private val selectDocTree = private val selectDocTree =
registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { uri ->
it ?: let { uri ?: let {
finish() finish()
return@registerForActivityResult return@registerForActivityResult
} }
if (it.isContentScheme()) { if (uri.isContentScheme()) {
val modeFlags = val modeFlags =
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
contentResolver.takePersistableUriPermission(it, modeFlags) contentResolver.takePersistableUriPermission(uri, modeFlags)
} }
onResult(Intent().setData(it)) onResult(Intent().setData(uri))
} }
private val selectDoc = registerForActivityResult(ActivityResultContracts.OpenDocument()) { private val selectDoc = registerForActivityResult(ActivityResultContracts.OpenDocument()) {
@ -185,10 +185,22 @@ class HandleFileActivity :
} }
override fun onResult(data: Intent) { override fun onResult(data: Intent) {
if (data.data != null) { val uri = data.data
setResult(RESULT_OK, data) uri ?: let {
finish()
return
}
if (mode == HandleFileContract.EXPORT) {
getFileData()?.let { fileData ->
viewModel.saveToLocal(uri, fileData.first, fileData.second) { savedUri ->
setResult(RESULT_OK, Intent().setData(savedUri))
finish()
}
} }
} else {
setResult(RESULT_OK, data)
finish() finish()
} }
}
} }

@ -1,9 +1,15 @@
package io.legado.app.ui.document package io.legado.app.ui.document
import android.app.Application import android.app.Application
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.help.DirectLinkUpload import io.legado.app.help.DirectLinkUpload
import io.legado.app.utils.FileUtils
import io.legado.app.utils.isContentScheme
import io.legado.app.utils.writeBytes
import java.io.File
class HandleFileViewModel(application: Application) : BaseViewModel(application) { class HandleFileViewModel(application: Application) : BaseViewModel(application) {
@ -20,4 +26,26 @@ class HandleFileViewModel(application: Application) : BaseViewModel(application)
} }
} }
fun saveToLocal(uri: Uri, fileName: String, data: ByteArray, success: (uri: Uri) -> Unit) {
execute {
return@execute if (uri.isContentScheme()) {
val doc = DocumentFile.fromTreeUri(context, uri)!!
doc.findFile(fileName)?.delete()
val newDoc = doc.createFile("", fileName)
newDoc!!.writeBytes(context, data)
newDoc.uri
} else {
val file = File(uri.path!!)
val newFile = FileUtils.createFileIfNotExist(file, fileName)
newFile.writeBytes(data)
Uri.fromFile(newFile)
}
}.onError {
it.printStackTrace()
errorLiveData.postValue(it.localizedMessage)
}.onSuccess {
success.invoke(it)
}
}
} }
Loading…
Cancel
Save