首页 > 解决方案 > Copy Drawable From ImageView

问题描述

I'm making an Android application and part of the application calls a Google API for photos for specific locations. These photos will be part of a list and I want to make it so once I call the API and download the photo, I can save it locally and resuse that later rather than recall the API. My code is pasted below

fun placePhotoCall(ref : String, view : ImageView, index : Int) {
    if(places[index].image == null) {
        Glide.with(this).load(createPhotosRequestURL(ref)).into(view)
        places[index].image = view.image
    }
    else {
        view.setImageDrawable(places[index].image)
    }
}

This function calls the API, downloads the photo, and stores it in the ImageView. I want to take the drawable image out of said ImageView and store it in an object I created however no matter what I try the image property remains null.

Thanks in advance.

标签: androidbitmapkotlindrawable

解决方案


不要存储可绘制对象的数组,尝试这样做的数据太多。只需存储用于加载该图像的 ref 字符串数组,并在需要的任何位置使用 ref 字符串重新加载图像。Glide 包已经缓存了图像,因此您只需将 ref 字符串传递给它,它只会在必要时再次下载图像。


推荐阅读