From 14bf8960ff5f0e61318b0ed0b37aecd2f069cc35 Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 31 Aug 2021 21:38:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/document/HandleFileActivity.kt | 26 ++++++++++++----- .../app/ui/document/HandleFileViewModel.kt | 28 +++++++++++++++++++ 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt b/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt index f1f1bee30..1bae24ae9 100644 --- a/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt +++ b/app/src/main/java/io/legado/app/ui/document/HandleFileActivity.kt @@ -31,17 +31,17 @@ class HandleFileActivity : private var mode = 0 private val selectDocTree = - registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { - it ?: let { + registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { uri -> + uri ?: let { finish() return@registerForActivityResult } - if (it.isContentScheme()) { + if (uri.isContentScheme()) { val modeFlags = 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()) { @@ -185,10 +185,22 @@ class HandleFileActivity : } override fun onResult(data: Intent) { - if (data.data != null) { + val uri = data.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() } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/document/HandleFileViewModel.kt b/app/src/main/java/io/legado/app/ui/document/HandleFileViewModel.kt index 66702b436..e6cbc718c 100644 --- a/app/src/main/java/io/legado/app/ui/document/HandleFileViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/document/HandleFileViewModel.kt @@ -1,9 +1,15 @@ package io.legado.app.ui.document import android.app.Application +import android.net.Uri +import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.MutableLiveData import io.legado.app.base.BaseViewModel 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) { @@ -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) + } + } + } \ No newline at end of file