android - 使用 Glide 将图像 URL 加载到 Kotlin AppWidget

标签 android kotlin android-glide

我在互联网上搜索了如何在 Kotlin 中加载远程图像的示例。有很多使用 Glide 和普通 ImageView 的 Kotlin 示例,但是没有任何与 AppWidget 中的远程 ImageViews 相关的例子。

目前最好的资源在这里,但它是用 Java 编写的: https://futurestud.io/tutorials/glide-loading-images-innnto-notifications-and-appwidgets

我在下面添加了 Kotlin 翻译...

最佳答案

class DeviceIntelWidget : AppWidgetProvider() {
    override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager?, 
    appWidgetIds: IntArray) {
        ...
        val remoteViews = RemoteViews(context.packageName, R.layout.widget)
        val awt: AppWidgetTarget = object : AppWidgetTarget(context.applicationContext, R.id.img, remoteViews, *appWidgetIds) {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                super.onResourceReady(resource, transition)
            }
        };    
        Glide.with(context.applicationContext).asBitmap().load("https://si.com/img.jpg").into(awt)
        ...
    }
}

注意事项:

  • AppWidgetTarget 用于通过 RemoteViews 在 AppWidget 的 ImageView 中显示下载的位图
  • 调用 load 加载位图并在 onResourceReady 中进行任何后期处理
  • 实现 'com.github.bumptech.glide:glide:4.2.0' 强制关闭并返回 NoClassDefFoundError: android.support.v4.util.ArrayMap
  • 更新到最新:4.7.1修复了问题

关于android - 使用 Glide 将图像 URL 加载到 Kotlin AppWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49882791/

相关文章:

android - 如何在不减慢 ListView 速度的情况下滑动显示本地视频缩略图?

android - Kotlin 中 Unresolved 异步引用

Android Studio 实时模板

android - Android 中的持久/离线图像缓存

android - 字符串末尾的正则表达式替换在 Android 版 Kotlin 中不起作用

kotlin - 使用RSA解密时出现BadPaddingException

java - 如何使用 Glide 通过字节数组加载图像?

java - 如何将可绘制资源中的图像保存到SD卡?

java - 创建 SQLite 数据库时出错 : "android.database.sqlite.SQLiteException: near(9598)"?

android - 如何向 Android SQLite 数据库添加两个新列?