pull/2121/head
kunfei 2 years ago
parent b8eedd2928
commit a70dd20c9e
  1. 34
      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.app.Application
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import io.legado.app.R 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.entities.TextChapter
import io.legado.app.ui.book.read.page.provider.ImageProvider import io.legado.app.ui.book.read.page.provider.ImageProvider
import io.legado.app.ui.book.searchContent.SearchResult import io.legado.app.ui.book.searchContent.SearchResult
import io.legado.app.utils.msg import io.legado.app.utils.*
import io.legado.app.utils.postEvent
import io.legado.app.utils.toStringArray
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch 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) { 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}")
}
} }
/** /**

Loading…
Cancel
Save