From a70dd20c9e96800a32edc08cec2c180f4a015ab8 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 2 Aug 2022 23:19:29 +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/book/read/ReadBookViewModel.kt | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index aabdac831..179c9d891 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -3,6 +3,7 @@ package io.legado.app.ui.book.read import android.app.Application import android.content.Intent import android.net.Uri +import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import io.legado.app.R @@ -29,12 +30,12 @@ import io.legado.app.service.BaseReadAloudService import io.legado.app.ui.book.read.page.entities.TextChapter import io.legado.app.ui.book.read.page.provider.ImageProvider import io.legado.app.ui.book.searchContent.SearchResult -import io.legado.app.utils.msg -import io.legado.app.utils.postEvent -import io.legado.app.utils.toStringArray -import io.legado.app.utils.toastOnUi +import io.legado.app.utils.* import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch +import java.io.File +import java.io.FileInputStream +import java.io.FileOutputStream /** * 阅读界面数据处理 @@ -426,8 +427,31 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } } + @Suppress("BlockingMethodInNonBlockingContext") fun saveImage(src: String?, uri: Uri) { - + src ?: return + val book = ReadBook.book ?: return + execute { + val image = BookHelp.getImage(book, src) + FileInputStream(image).use { input -> + if (uri.isContentScheme()) { + DocumentFile.fromTreeUri(context, uri)?.let { doc -> + DocumentUtils.createFileIfNotExist(doc, image.name) + context.contentResolver.openOutputStream(doc.uri)!!.use { output -> + input.copyTo(output) + } + } + } else { + val dir = File(uri.path ?: uri.toString()) + val file = FileUtils.createFileIfNotExist(dir, image.name) + FileOutputStream(file).use { output -> + input.copyTo(output) + } + } + } + }.onError { + context.toastOnUi("保存图片出错\n${it.localizedMessage}") + } } /**