Android用Coil 3检查媒体资源是否有效,Kotlin
Kotlin
@WorkerThread
fun checkImage(ctx: Context, uri: Uri): Boolean {
val t = System.currentTimeMillis()
val request = ImageRequest.Builder(ctx)
.data(uri)
.memoryCacheKey(uri.toString())
.precision(Precision.INEXACT)
.bitmapConfig(Bitmap.Config.ALPHA_8)
.size(1, 1)
.build()
val result = coil3.executeBlocking(request)
val bmp = result.image?.toBitmap()
var valid = false
if (bmp != null && bmp.byteCount > 0) {
valid = true
}
Log.d("fly", "image valid=$valid time=${System.currentTimeMillis() - t}ms")
return valid
}
返回true,表明这个uri指向的媒体是有效的资源。