From bfc581092f6ded30f95dc833741c327f94d395b4 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 7 Mar 2020 21:46:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/http/HttpHelper.kt | 10 ++++++++++ .../legado/app/ui/rss/read/ReadRssViewModel.kt | 16 ++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index a37e053fb..df417e2f9 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -57,6 +57,16 @@ object HttpHelper { return null } + suspend fun simpleGetByteAsync(url: String): ByteArray? { + NetworkUtils.getBaseUrl(url)?.let { baseUrl -> + return getByteRetrofit(baseUrl) + .create(HttpGetApi::class.java) + .getMapByteAsync(url, mapOf(), mapOf()) + .body() + } + return null + } + inline fun getApiService(baseUrl: String, encode: String? = null): T { return getRetrofit(baseUrl, encode).create(T::class.java) } diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt index 7cc96ef29..6d90fd181 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt @@ -6,6 +6,7 @@ import android.net.Uri import android.speech.tts.TextToSpeech import android.speech.tts.UtteranceProgressListener import android.util.Base64 +import android.webkit.URLUtil import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.MutableLiveData import io.legado.app.App @@ -14,6 +15,7 @@ import io.legado.app.base.BaseViewModel import io.legado.app.constant.AppConst import io.legado.app.data.entities.RssArticle import io.legado.app.data.entities.RssSource +import io.legado.app.help.http.HttpHelper import io.legado.app.model.Rss import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.utils.DocumentUtils @@ -99,7 +101,7 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application), webPic ?: return execute { val fileName = "${AppConst.fileNameFormat.format(Date(System.currentTimeMillis()))}.jpg" - webData2bitmap(webPic).let { biteArray -> + webData2bitmap(webPic)?.let { biteArray -> if (path.isContentPath()) { val uri = Uri.parse(path) DocumentFile.fromTreeUri(context, uri)?.let { doc -> @@ -110,14 +112,20 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application), val file = FileUtils.createFileIfNotExist(File(path), fileName) file.writeBytes(biteArray) } - } + } ?: throw Throwable("NULL") }.onError { toast("保存图片失败:${it.localizedMessage}") + }.onSuccess { + toast("保存成功") } } - private fun webData2bitmap(data: String): ByteArray { - return Base64.decode(data.split(",").toTypedArray()[1], Base64.DEFAULT) + private suspend fun webData2bitmap(data: String): ByteArray? { + return if (URLUtil.isValidUrl(data)) { + HttpHelper.simpleGetByteAsync(data) + } else { + Base64.decode(data.split(",").toTypedArray()[1], Base64.DEFAULT) + } } fun clHtml(content: String): String {