优化
This commit is contained in:
@@ -6,7 +6,6 @@ import android.graphics.drawable.Drawable
|
|||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.util.Base64
|
import android.util.Base64
|
||||||
import androidx.annotation.DrawableRes
|
import androidx.annotation.DrawableRes
|
||||||
import com.bumptech.glide.Glide
|
|
||||||
import com.bumptech.glide.RequestBuilder
|
import com.bumptech.glide.RequestBuilder
|
||||||
import io.legado.app.constant.AppPattern.dataUriRegex
|
import io.legado.app.constant.AppPattern.dataUriRegex
|
||||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||||
@@ -23,11 +22,11 @@ object ImageLoader {
|
|||||||
fun load(context: Context, path: String?): RequestBuilder<Drawable> {
|
fun load(context: Context, path: String?): RequestBuilder<Drawable> {
|
||||||
val dataUriFindResult = dataUriRegex.find(path ?: "")
|
val dataUriFindResult = dataUriRegex.find(path ?: "")
|
||||||
return when {
|
return when {
|
||||||
path.isNullOrEmpty() -> Glide.with(context).load(path)
|
path.isNullOrEmpty() -> GlideApp.with(context).load(path)
|
||||||
dataUriFindResult != null -> kotlin.runCatching {
|
dataUriFindResult != null -> kotlin.runCatching {
|
||||||
val dataUriBase64 = dataUriFindResult.groupValues[1]
|
val dataUriBase64 = dataUriFindResult.groupValues[1]
|
||||||
val byteArray = Base64.decode(dataUriBase64, Base64.DEFAULT)
|
val byteArray = Base64.decode(dataUriBase64, Base64.DEFAULT)
|
||||||
Glide.with(context).load(byteArray)
|
GlideApp.with(context).load(byteArray)
|
||||||
}.getOrDefault(
|
}.getOrDefault(
|
||||||
GlideApp.with(context).load(path)
|
GlideApp.with(context).load(path)
|
||||||
)
|
)
|
||||||
@@ -37,63 +36,64 @@ object ImageLoader {
|
|||||||
}.getOrDefault(path)
|
}.getOrDefault(path)
|
||||||
GlideApp.with(context).load(url)
|
GlideApp.with(context).load(url)
|
||||||
}
|
}
|
||||||
path.isContentScheme() -> Glide.with(context).load(Uri.parse(path))
|
path.isContentScheme() -> GlideApp.with(context).load(Uri.parse(path))
|
||||||
else -> kotlin.runCatching {
|
else -> kotlin.runCatching {
|
||||||
Glide.with(context).load(File(path))
|
GlideApp.with(context).load(File(path))
|
||||||
}.getOrElse {
|
}.getOrElse {
|
||||||
Glide.with(context).load(path)
|
GlideApp.with(context).load(path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun loadBitmap(context: Context, path: String?): RequestBuilder<Bitmap> {
|
fun loadBitmap(context: Context, path: String?): RequestBuilder<Bitmap> {
|
||||||
return when {
|
return when {
|
||||||
path.isNullOrEmpty() -> Glide.with(context).asBitmap().load(path)
|
path.isNullOrEmpty() -> GlideApp.with(context).asBitmap().load(path)
|
||||||
path.isAbsUrl() -> Glide.with(context).asBitmap().load(AnalyzeUrl(path).getGlideUrl())
|
path.isAbsUrl() -> GlideApp.with(context).asBitmap()
|
||||||
path.isContentScheme() -> Glide.with(context).asBitmap().load(Uri.parse(path))
|
.load(AnalyzeUrl(path).getGlideUrl())
|
||||||
|
path.isContentScheme() -> GlideApp.with(context).asBitmap().load(Uri.parse(path))
|
||||||
else -> kotlin.runCatching {
|
else -> kotlin.runCatching {
|
||||||
Glide.with(context).asBitmap().load(File(path))
|
GlideApp.with(context).asBitmap().load(File(path))
|
||||||
}.getOrElse {
|
}.getOrElse {
|
||||||
Glide.with(context).asBitmap().load(path)
|
GlideApp.with(context).asBitmap().load(path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun loadFile(context: Context, path: String?): RequestBuilder<File> {
|
fun loadFile(context: Context, path: String?): RequestBuilder<File> {
|
||||||
return when {
|
return when {
|
||||||
path.isNullOrEmpty() -> Glide.with(context).asFile().load(path)
|
path.isNullOrEmpty() -> GlideApp.with(context).asFile().load(path)
|
||||||
path.isAbsUrl() -> Glide.with(context).asFile().load(AnalyzeUrl(path).getGlideUrl())
|
path.isAbsUrl() -> GlideApp.with(context).asFile().load(AnalyzeUrl(path).getGlideUrl())
|
||||||
path.isContentScheme() -> Glide.with(context).asFile().load(Uri.parse(path))
|
path.isContentScheme() -> GlideApp.with(context).asFile().load(Uri.parse(path))
|
||||||
else -> kotlin.runCatching {
|
else -> kotlin.runCatching {
|
||||||
Glide.with(context).asFile().load(File(path))
|
GlideApp.with(context).asFile().load(File(path))
|
||||||
}.getOrElse {
|
}.getOrElse {
|
||||||
Glide.with(context).asFile().load(path)
|
GlideApp.with(context).asFile().load(path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun load(context: Context, @DrawableRes resId: Int?): RequestBuilder<Drawable> {
|
fun load(context: Context, @DrawableRes resId: Int?): RequestBuilder<Drawable> {
|
||||||
return Glide.with(context).load(resId)
|
return GlideApp.with(context).load(resId)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun load(context: Context, file: File?): RequestBuilder<Drawable> {
|
fun load(context: Context, file: File?): RequestBuilder<Drawable> {
|
||||||
return Glide.with(context).load(file)
|
return GlideApp.with(context).load(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun load(context: Context, uri: Uri?): RequestBuilder<Drawable> {
|
fun load(context: Context, uri: Uri?): RequestBuilder<Drawable> {
|
||||||
return Glide.with(context).load(uri)
|
return GlideApp.with(context).load(uri)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun load(context: Context, drawable: Drawable?): RequestBuilder<Drawable> {
|
fun load(context: Context, drawable: Drawable?): RequestBuilder<Drawable> {
|
||||||
return Glide.with(context).load(drawable)
|
return GlideApp.with(context).load(drawable)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun load(context: Context, bitmap: Bitmap?): RequestBuilder<Drawable> {
|
fun load(context: Context, bitmap: Bitmap?): RequestBuilder<Drawable> {
|
||||||
return Glide.with(context).load(bitmap)
|
return GlideApp.with(context).load(bitmap)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun load(context: Context, bytes: ByteArray?): RequestBuilder<Drawable> {
|
fun load(context: Context, bytes: ByteArray?): RequestBuilder<Drawable> {
|
||||||
return Glide.with(context).load(bytes)
|
return GlideApp.with(context).load(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -10,7 +10,8 @@ import java.io.InputStream
|
|||||||
|
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
@GlideModule
|
@GlideModule
|
||||||
class OkHttpGlideModule : AppGlideModule() {
|
class LegadoGlideModule : AppGlideModule() {
|
||||||
|
|
||||||
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
|
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
|
||||||
registry.replace(
|
registry.replace(
|
||||||
GlideUrl::class.java,
|
GlideUrl::class.java,
|
||||||
@@ -18,4 +19,5 @@ class OkHttpGlideModule : AppGlideModule() {
|
|||||||
OkHttpModeLoaderFactory
|
OkHttpModeLoaderFactory
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -35,10 +35,8 @@ class OkHttpStreamFetcher(private val url: GlideUrl) :
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun cleanup() {
|
override fun cleanup() {
|
||||||
try {
|
kotlin.runCatching {
|
||||||
stream?.close()
|
stream?.close()
|
||||||
} catch (e: IOException) {
|
|
||||||
// Ignored
|
|
||||||
}
|
}
|
||||||
responseBody?.close()
|
responseBody?.close()
|
||||||
callback = null
|
callback = null
|
||||||
@@ -65,9 +63,9 @@ class OkHttpStreamFetcher(private val url: GlideUrl) :
|
|||||||
if (response.isSuccessful) {
|
if (response.isSuccessful) {
|
||||||
val contentLength: Long = Preconditions.checkNotNull(responseBody).contentLength()
|
val contentLength: Long = Preconditions.checkNotNull(responseBody).contentLength()
|
||||||
stream = ContentLengthInputStream.obtain(responseBody!!.byteStream(), contentLength)
|
stream = ContentLengthInputStream.obtain(responseBody!!.byteStream(), contentLength)
|
||||||
callback!!.onDataReady(stream)
|
callback?.onDataReady(stream)
|
||||||
} else {
|
} else {
|
||||||
callback!!.onLoadFailed(HttpException(response.message, response.code))
|
callback?.onLoadFailed(HttpException(response.message, response.code))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user