0
0

More than 1 year has passed since last update.

[Comopse]Coilを使ってString -> Bitmap -> Drawableに型変換する方法

Posted at

こんな時

  • StringをどうしてもBitmapの形に変換したい時
  • BitmapをどうしてもDrawableの形に変換したい時
  • StringをどうしてもDrawableの形に変換したい時

前提

  • Composable関数の中で書く
  • coilを使うのでgradleに以下追加
implementation("io.coil-kt:coil-compose:2.1.0")

コード

コルーチンの中で変換します。
以下は String -> Bitmap -> Drawableの順に変換してます。

val myString = "画像URLとか"
val context = LocalContext.current

val coroutineScope = rememberCoroutineScope()
coroutineScope.launch{
val loader = ImageLoader(context)
val request = ImageRequest.Builder(context)
    .data(myString)
    .allowHardware(false) 
    .build()

val result = (loader.execute(request) as SuccessResult).drawable
val myBitmap = (result as BitmapDrawable).bitmap

val myDrawable = context.imageLoader
    .execute(ImageRequest.Builder(context).data(myBitmap).build())
    .drawable
}

myBitmapがbitmapの形になってます。
myDrawableがDrawableの形になってます。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0