0

I'm already get images into my recycle view. All the other details were saved into sqlite database. But there is a problem with saving image.

this is my database table and method for save details into data base

val CREATE_TABLE = "CREATE TABLE $TABLE_NAME " + "($ID INTEGER PRIMARY KEY, $BOOK_NAME TEXT, $DESCRIPTION TEXT, $AUTHOR TEXT, $COVER_PAGE BLOB, $ISBN TEXT, $LANGUAGE TEXT, $NUMBER_OF_PAGES INTEGER, $PRICE TEXT, $CATEGORY TEXT)" db?.execSQL(CREATE_TABLE)

fun addFavouriteBooks(
        id: Int,
        bookName: String,
        description: String,
        author: String,
        cover: ByteArray,
        isbn: String,
        language: String,
        numbOfPages: Int,
        price: String,
        category: String
    ): Long {
        val db = this.writableDatabase
        val contentValues = ContentValues()
        contentValues.put(ID, id)
        contentValues.put(BOOK_NAME, bookName)
        contentValues.put(DESCRIPTION, description)
        contentValues.put(AUTHOR, author)
        contentValues.put(COVER_PAGE, cover)
        contentValues.put(ISBN, isbn)
        contentValues.put(LANGUAGE, language)
        contentValues.put(NUMBER_OF_PAGES, numbOfPages)
        contentValues.put(PRICE, price)
        contentValues.put(CATEGORY, category)

        val res = db.insert(TABLE_NAME, null, contentValues)
        db.close()
        return res
    }

Here is my function in recycle adapter to catch the data in recycle view and sed it to the sqlite database. I haven't any idea to send image like other details.


        holder.like.setOnClickListener {
            var c_id = holder.id
            var c_name = holder.name.text.toString()
            var c_description = holder.description
            var c_author = holder.author.text.toString()
            var c_cover = holder.cover.toString()

            //i want to convert this c_cover into byte array

            var c_isbn = holder.isbn
            var c_lang = holder.language
            var c_numberOfPages: Int = holder.numbOfPages.toInt()
            var c_price = holder.price.text.toString()
            var c_category = holder.category

            var favList: Long = dbHelper.addFavouriteBooks(c_id.toInt(), c_name, c_description, c_author,c_cover, c_isbn, c_lang, c_numberOfPages, c_price, c_category)

            if(favList > 0)
            {
                Toast.makeText(context, "Book added to favourites", Toast.LENGTH_SHORT).show()
            }
        }
3
  • 1
    first you get the bitmap of your image then convert it to byte[] Commented Oct 24, 2019 at 0:29
  • can u send me the code for that conversion? I tried several methods but not working. Commented Oct 25, 2019 at 12:08
  • I'm not familiar with kotlin yet. I'll send the code in java Commented Oct 28, 2019 at 0:44

1 Answer 1

1

This how we do it in java.

byte[] Imagebyte = getBitmapAsByteArray(((BitmapDrawable) YOUR_IMAGEVIEW.getDrawable()).getBitmap());

Then we store it in sqlite in FIELD's with data type BLOB.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you. If you are not familiar with kotlin yet you can use java kotlin converter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.