Kotlin 中如何使用 Picasso 将 URL 转换为位图?
Posted
技术标签:
【中文标题】Kotlin 中如何使用 Picasso 将 URL 转换为位图?【英文标题】:How to use Picasso to convert URL to Bitmap in Kotlin? 【发布时间】:2021-11-18 11:14:01 【问题描述】:我想在 google api 中更改我的标记。 所以我用毕加索的“getBitmap”功能 但是这个函数返回“位图?”
.icon(BitmapDescriptorFactory.fromBitmap(getBitmap(user_URL[i]))) -> 需要“位图”类型
所以,问题是:
类型不匹配错误(需要:位图,找到:位图?) java.lang.NullPointerException:同一行中的图像不能为空
如何在 kotlin 中解决这个问题?
代码:
私人乐趣 getBitmap(url : String) : 位图?
var bmp : Bitmap ?=null
Picasso.get().load(url).into(object : com.squareup.picasso.Target
override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?)
bmp = bitmap
override fun onPrepareLoad(placeHolderDrawable: Drawable?)
override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?)
)
return bmp
私人乐趣 otherUserMaker(googleMap: GoogleMap)
var latitude = mutableListOf<Double>()
var longitude = mutableListOf<Double>()
var user_URL = mutableListOf<String>()enter code here
user_URL = maprepo.returnImage()
latitude = maprepo.returnLatitude()
longitude = maprepo.returnLongitude()
for (i in 0 until latitude.size step (1))
val makerOptions = MarkerOptions()
makerOptions
.position(LatLng(latitude[i], longitude[i]))
.title("") .icon(BitmapDescriptorFactory.fromBitmap(getBitmap(user_URL[i])))
googleMap.addMarker(makerOptions)
firebase 中的 imageURL 类型 mustablearray[String]
和maprepo图像功能是
fun getImage(boardDTOId: MutableList<String>)
for (i in boardDTOId)
firestore.collection("Board").document(i).get().addOnSuccessListener
if (it != null)
photoUrl.add(it["profileUrl"] as String)
有趣的 returnImage(): MutableList 返回照片网址
================================================ ==============================
user_URL 数据集for (i in 0 until latitude.size step (1)) Log.e("user_Url", user_URL.toString()) Log.e("user_Url1", user_URL[i])
-> 日志
021-09-26 03:12:36.810 20861-20861/com.example.myapplication E/user_Url:[https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o /Profiles%2FJPEG_20210925_202119_.png?alt=media&token=2b35d88f-c748-4faf-bc5b-a809294a4d4f,https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o/Profiles%2FJPEG_20210925_2021.png ?alt=media&token=2b35d88f-c748-4faf-bc5b-a809294a4d4f, https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o/Profiles%2FJPEG_20210925_195916_.png?alt=media&token=1fbae93b -85d4-447d-a8f8-008110423616,https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o/Profiles%2FJPEG_20210922_200319_.png?alt=media&token=b0a27498-382e-4cee-b268 -1a7ba89ad0e7]
2021-09-26 03:12:36.810 20861-20861/com.example.myapplication E/user_Url1:https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o/Profiles%2FJPEG_20210925_202119_.png?alt=media&token=2b35d88f-c748-4faf-bc5b-a809294a4d4f
2021-09-26 03:12:36.819 20861-20861/com.example.myapplication E/user_Url:[https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o /Profiles%2FJPEG_20210925_202119_.png?alt=media&token=2b35d88f-c748-4faf-bc5b-a809294a4d4f,https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o/Profiles%2FJPEG_20210925_2021.png ?alt=media&token=2b35d88f-c748-4faf-bc5b-a809294a4d4f, https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o/Profiles%2FJPEG_20210925_195916_.png?alt=media&token=1fbae93b -85d4-447d-a8f8-008110423616,https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o/Profiles%2FJPEG_20210922_200319_.png?alt=media&token=b0a27498-382e-4cee-b268 -1a7ba89ad0e7]
2021-09-26 03:12:36.819 20861-20861/com.example.myapplication E/user_Url1:https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o/Profiles%2FJPEG_20210925_202119_.png?alt=media&token=2b35d88f-c748-4faf-bc5b-a809294a4d4f
2021-09-26 03:12:36.832 20861-20861/com.example.myapplication E/user_Url:[https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o /Profiles%2FJPEG_20210925_202119_.png?alt=media&token=2b35d88f-c748-4faf-bc5b-a809294a4d4f,https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o/Profiles%2FJPEG_20210925_2021.png ?alt=media&token=2b35d88f-c748-4faf-bc5b-a809294a4d4f, https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o/Profiles%2FJPEG_20210925_195916_.png?alt=media&token=1fbae93b -85d4-447d-a8f8-008110423616,https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o/Profiles%2FJPEG_20210922_200319_.png?alt=media&token=b0a27498-382e-4cee-b268 -1a7ba89ad0e7]
2021-09-26 03:12:36.832 20861-20861/com.example.myapplication E/user_Url1:https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o/Profiles%2FJPEG_20210925_195916_.png?alt=media&token=1fbae93b-85d4-447d-a8f8-008110423616
2021-09-26 03:12:36.843 20861-20861/com.example.myapplication E/user_Url:[https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o /Profiles%2FJPEG_20210925_202119_.png?alt=media&token=2b35d88f-c748-4faf-bc5b-a809294a4d4f,https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o/Profiles%2FJPEG_20210925_2021.png ?alt=media&token=2b35d88f-c748-4faf-bc5b-a809294a4d4f, https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o/Profiles%2FJPEG_20210925_195916_.png?alt=media&token=1fbae93b -85d4-447d-a8f8-008110423616,https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o/Profiles%2FJPEG_20210922_200319_.png?alt=media&token=b0a27498-382e-4cee-b268 -1a7ba89ad0e7]
2021-09-26 03:12:36.843 20861-20861/com.example.myapplication E/user_Url1:https://firebasestorage.googleapis.com/v0/b/capston-e8850.appspot.com/o/Profiles%2FJPEG_20210922_200319_.png?alt=media&token=b0a27498-382e-4cee-b268-1a7ba89ad0e7
【问题讨论】:
分享你的代码。 @jyk 尝试以文本格式而不是图像发布代码 检查这个Answer,我想这会对你有所帮助 【参考方案1】:简短而简单的方法是
val bitmap: Bitmap = Picasso.with(context).load(url).get()
【讨论】:
感谢 andswer 但“java.lang.IllegalStateException:方法调用不应该从主线程发生。”在这里 你的图片是空的,它没有初始化 以文本形式发布所有代码和错误 我上传了我的代码 图像声明在哪里以上是关于Kotlin 中如何使用 Picasso 将 URL 转换为位图?的主要内容,如果未能解决你的问题,请参考以下文章
Flutter : Picasso 或 Universal Image loader 等价物