0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Android studio】ImageViewへ容量の大きい画像を読み込む方法

Posted at

状況

実機で操作時にImageViewへ2MB以上のjpg画像を挿入しようとしたところ、アプリが落ちてしまった。
メモリ不足に陥っている可能性が考えられた。

対処方法

1. jpg画像をBitmapに変換、
2. Bitmapのサイズを調整。
3. ImageViewに張り付ける。

MainActivity.kt
val bitmap = adjustImage(resources, R.drawable.imagefilename)
menuImage.setImageBitmap(bitmap)    //・・・3

private fun adjustImage(res: Resources, resID: Int): Bitmap {
    var bmp = BitmapFactory.decodeResource(res, resID)     //・・・1
    return createScaledBitmap(bmp, 320, 264, true)    //・・・2
}

懸念事項

BitmapFactory.decodeResource(res, resID)で容量の大きいjpg画像を読んでいるから、その際にもメモリを消費しているのでは?
本当は下記URLのようにBitmapとして読み込む前にリサイズしたいのだが、自分ではうまくいかず。
kotlinの理解が進んできたら、再度見直すこととしたい。
参考URL:https://developer.android.com/topic/performance/graphics/load-bitmap?hl=ja

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?