无法从 ImageView 获取位图作为 BitmapDrawable
Can't get Bitmap from ImageView as a BitmapDrawable
在我的 Android - Kotlin 应用程序中,我试图从图像中获取位图以使用它 colorPallete
,这是我的 [=15] 中的 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
returns 一个 URL
在此之后,当我尝试通过这种方式从 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
这应该可以解决您的问题:
val bitmap = (mDressImage1?.drawable as? BitmapDrawable)?.bitmap
在我的 Android - Kotlin 应用程序中,我试图从图像中获取位图以使用它 colorPallete
,这是我的 [=15] 中的 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
returns 一个 URL
在此之后,当我尝试通过这种方式从 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
这应该可以解决您的问题:
val bitmap = (mDressImage1?.drawable as? BitmapDrawable)?.bitmap