---
# System prepended metadata

title: Images.getBitmap() 方法報錯
tags: [Android Studio, 除錯記錄, Kotlin]

---

# Images.getBitmap() 方法報錯

## 問題描述
`MediaStore.Images.getBitmap()` 方法報錯：unresolved reference getbitmap

![](https://i.imgur.com/aHt8B3N.png)


## 解決方案
官網上說 `Images.getBitmap()` 方法已在 Api 29 棄用。:cry: 
要先用 `ImageDecoder.createSource(cr : ContentResolver, uri :  Uri)` 建立圖片資源，再轉換成 bitmap 或 Drawable 型態。

```kotlin
val source = ImageDecoder.createSource(this.contentResolver, uri!!)
val bitmapDrawable = ImageDecoder.decodeDrawable(source)
btn_photo_register.background = bitmapDrawable
```

## ref
https://developer.android.com/reference/android/provider/MediaStore.Images.Media#getBitmap(android.content.ContentResolver,%20android.net.Uri)

https://developer.android.com/reference/android/graphics/ImageDecoder?hl=en#createSource(android.content.ContentResolver,%20android.net.Uri)

###### tags: `Kotlin` `除錯記錄` `Android Studio`