Realmで作ったテーブルに画像を保存してimageViewに表示させたいのですがうまくいきません。
エラーメッセージ
Field "recipeImage" of type "android.graphics.Bitmap" is not supported.
これをどう解決すればよいのかわかりませんでした。
詳しい方ご教授ください。
kotlin
1 2import android.arch.lifecycle.Transformations.map 3import android.graphics.Bitmap 4import android.graphics.BitmapFactory 5import android.os.Bundle 6import android.support.v7.app.AppCompatActivity 7import android.view.View 8import android.widget.ListView 9import io.realm.Realm 10import io.realm.kotlin.createObject 11import io.realm.kotlin.where 12 13import java.util.* 14import android.widget.SimpleAdapter 15import io.realm.kotlin.delete 16import kotlinx.android.synthetic.main.recipelayout.* 17import java.io.ByteArrayOutputStream 18import kotlin.collections.ArrayList 19 20 21class MainActivity : AppCompatActivity() { 22 private lateinit var realm: Realm 23 24 override fun onCreate(savedInstanceState: Bundle?) { 25 26 realm = Realm.getDefaultInstance() 27 realm.executeTransaction() { 28 val delete = realm.where<Recipe>().contains("recipeName" , "curry").findAll() 29 delete.deleteAllFromRealm() 30 } 31 realm.beginTransaction() 32 val bmp = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher) 33 realm.createObject<Recipe>(UUID.randomUUID().toString()).apply { 34 recipeName = "curry" 35 recipeHowtomake = "じゃがいも切って人参きって肉きって鍋に入れて水いれてカレールー入れて煮て終わりです。" 36 recipeImage = bmp 37 } 38 realm.commitTransaction() 39 var listView: ListView 40 super.onCreate(savedInstanceState) 41 setContentView(R.layout.activity_main) 42 43 listView = findViewById(R.id.listView) 44 val Recipe = realm.where<Recipe>().findAll() 45 listView?.adapter = RecipeAdapter(Recipe) 46 } 47// fun createImageData() : ByteArray { 48// val bmp = (re_image.drawable as ).bmp 49// val baos = ByteArrayOutputStream() 50// bmp.compress(Bitmap.CompressFormat.PNG, 100, baos) 51// val imageByteArray = baos.toByteArray() 52// return imageByteArray 53// 54// } 55 56 override fun onDestroy() { 57 super.onDestroy() 58 realm.close() 59 } 60} 61
kotlin
1 2import android.graphics.Bitmap 3import android.view.View 4import io.realm.RealmObject 5import io.realm.annotations.PrimaryKey 6import java.io.Serializable 7 8open class Recipe : Serializable, RealmObject() { 9 @PrimaryKey 10 var recipeId: String? = null 11 var recipeName: String? = null 12 var recipeCategory: String? = null 13 var recipeHowtomake: String? = null 14 var recipeInfo: String? = null 15 var recipeNinmae: Int = 0 16 var recipePrice: Int = 0 17 var recipeMaterial: String? = null 18 var recipeImage: Bitmap? = null 19 20 } 21 22 23 24 25
kotlin
1 2import android.graphics.Bitmap 3import android.view.LayoutInflater 4import android.view.View 5import android.view.ViewGroup 6import android.widget.ImageView 7import android.widget.ListView 8import android.widget.TextView 9import io.realm.OrderedRealmCollection 10import io.realm.RealmBaseAdapter 11import kotlinx.android.synthetic.main.recipelayout.view.* 12 13class RecipeAdapter (data: OrderedRealmCollection<Recipe>?) 14 : RealmBaseAdapter<Recipe>(data) { 15 16 17 inner class ViewHolder(cell : View) { 18 var recipeName = cell.findViewById<TextView>(R.id.re_name) 19 var recipeHowtomake = cell.findViewById<TextView>(R.id.re_Howtomake) 20 var recipeImage = cell.findViewById<ImageView>(R.id.re_image) 21 } 22 23 24 override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { 25 val view : View 26 val viewHolder : ViewHolder 27 28 when(convertView) { 29 null -> { 30 val inflater = LayoutInflater.from(parent?.context) 31 view = inflater.inflate(R.layout.recipelayout,parent,false) 32 viewHolder = ViewHolder(view) 33 view.tag = viewHolder 34 } 35 else -> { 36 view = convertView 37 viewHolder = view.tag as ViewHolder 38 } 39 } 40 adapterData?.run { 41 val Recipe = get(position) 42 viewHolder.recipeName.text = Recipe.recipeName 43 viewHolder.recipeHowtomake.text = Recipe.recipeHowtomake 44 viewHolder.recipeImage.setImageBitmap(Recipe.recipeImage) 45 } 46 return view 47 } 48}
あなたの回答
tips
プレビュー