質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Realm

RealmとはSQLiteやCore Dataに代わるモバイルデータベースです。iOSとAndroidの両方でサポートされています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

0回答

2437閲覧

Realmに画像を保存する方法 URlパスを指定する方法

telin

総合スコア18

Realm

RealmとはSQLiteやCore Dataに代わるモバイルデータベースです。iOSとAndroidの両方でサポートされています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2018/11/13 05:05

編集2018/11/16 00:49

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}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問