【问题标题】:Can't get Bitmap from ImageView as a BitmapDrawable无法从 ImageView 获取位图作为 BitmapDrawable
【发布时间】:2020-02-28 11:19:32
【问题描述】:

在我的 Android - Kotlin 应用程序中,我试图从图像中获取位图以使用它的colorPallete,这是我的XML 文件中的ImageView

        <ImageView
            android:id="@+id/dressImage_1"
            android:layout_width="142dp"
            android:layout_height="123dp"
            android:contentDescription="@string/app_name"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/icon" />

这是我添加图片的方法:

Picasso.get().load(dress.image1).into(mDressImage1)

到这里一切顺利。

注意dress.image1返回一个网址

在此之后,当我尝试以这种方式从imageView 获取bitmap 时:

        val bitmap = (mDressImage1?.drawable as BitmapDrawable).bitmap

它只是崩溃了。 logcat 什么都没有

更新 它给了我错误null cannot be cast to non-null type android.graphics.drawable.BitmapDrawable

指向val bitmap = (mDressImage1?.drawable as BitmapDrawable).bitmap这一行

【问题讨论】:

标签: android kotlin picasso android-bitmap


【解决方案1】:

这应该可以解决您的问题:

val bitmap = (mDressImage1?.drawable as? BitmapDrawable)?.bitmap

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 2012-12-20
    相关资源
    最近更新 更多