【问题标题】:Load image from Url without third-party在没有第三方的情况下从 Url 加载图像
【发布时间】:2021-10-27 19:41:42
【问题描述】:

我正在尝试使用此代码从 URL 加载图像:

@BindingAdapter("imageUrl")
fun ImageView.bindImage(imgUrl: String?) {

    imgUrl?.let {
        val imgUri = imgUrl.toUri().buildUpon().scheme("https").build().toString()
        val executor = Executors.newSingleThreadExecutor()
        val handler = Handler(Looper.getMainLooper())
        var image: Bitmap?
        executor.execute {
            try {
                val `in` = URL(imgUri).openStream()
                image = BitmapFactory.decodeStream(`in`)

                handler.post {
                    this.setImageBitmap(image)
                    executor.shutdown()
                }
            } catch (e: Exception) {
                Log.e("Tag", "onCreate: ${e.message}")
            }
        }
    }
}

这是xml

 <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/iv_album_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:imageUrl="@{albumsObject.albumImage}"
                android:src="@drawable/img_album_cover" />

它工作得很好..但是有问题 1-当我运行模拟器时,笔记本电脑正在升温并且风扇变得非常响亮 2-如果我在回收站列表上滚动查看缓慢绑定的图像,我可以在下一张照片上看到上一张照片

这段代码有问题吗?或者有任何人有另一种方式从 URL 加载图像而不使用任何第三方 .. 只是 Android SDK

【问题讨论】:

  • I can see the previous photo on the upcoming photo 然后先使用setImageBitmap(null);。

标签: android image kotlin url data-binding


【解决方案1】:

试试这个

fun ImageView.loadImage(url: String?){
    val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
    StrictMode.setThreadPolicy(policy)
    val bitmap: Bitmap?
    val inputStream: InputStream
    try {
        inputStream = URL(url).openStream()
        bitmap = BitmapFactory.decodeStream(inputStream)
    } catch (e: IOException) {
        e.printStackTrace()
        return
    }
    this.setImageBitmap(bitmap)
}

【讨论】:

  • 请不要使用严格模式。它的错误编码和块。使用线程。
【解决方案2】:

我用这段代码解决了这个问题:

@BindingAdapter("imageUrl")
fun ImageView.bindImage(imgUrl: String?) {

    if(this.drawable == null) {

        imgUrl?.let {
            val imgUri = imgUrl.toUri().buildUpon().scheme("https").build().toString()


            val uiHandler = Handler(Looper.getMainLooper())
            thread(start = true) {
                val bitmap = downloadBitmap(imgUri)
                uiHandler.post {
                    this.setImageBitmap(bitmap)
                }
            }
        }
    }

}

fun downloadBitmap(imageUrl: String): Bitmap? {
    return try {
        val conn = URL(imageUrl).openConnection()
        conn.connect()
        val inputStream = conn.getInputStream()
        val bitmap = BitmapFactory.decodeStream(inputStream)
        inputStream.close()
        bitmap
    } catch (e: Exception) {
        Log.e(ContentValues.TAG, "Exception $e")
        null
    }
}
  1. 检查是否尚未加载,避免加载多个。
  2. 将 HTTPS 添加到 URL。
  3. 创建线程以打开输入流连接并获取图像位图。
  4. 创建处理程序以返回主线程并设置图像位图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 2010-10-26
    相关资源
    最近更新 更多