Home > Net >  When updating/deleting entry in RoomDatabase and then creating new entry App crashes
When updating/deleting entry in RoomDatabase and then creating new entry App crashes

Time:06-20

I work on an app where I have a recyclerview and a fab on the first fragment (listFragment) you see when you open the app. you click the fab, camera opens, you make a picture -> click ok and get to another fragment, there you fill out some stuff and then press save. you get back to the recyclerview fragment and now you can see the saved entry (room database). when you click the listItem you created before you get to an update fragment where you can either update stuff or delete it. now before it all worked fine. But now with help of a yt video I tried to also insert a bitmap into the database. That works (@TypeConverter) and now I also have the right bitmap displayed in the listItem and the updateFragment.

The problem: When I click on a listItem and in the updateFragment I click the update button or delete icon that works fine and I get back to the listFragment. But when I then click on the fab in order to create a new entry the camera opens and then the app crashes (and the camera stays open).

Even when the delete and update code is commented out and the only thing that happens when you click these buttons is the navigation between fragments it still crashes but if I remove all the bitmap database code it is fine again. I can delete and/or update multiple entries no problem but when I click the fab and camera starts the app crashes. I also use safeargs for transportation of data to updateFragment when I click the listItem. The app also crashes when I delete/update something and then leave the app by pressing the homebutton on the phone. I navigate out of the app and then get the "app crashed" message. LOGCAT:

java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 4675628 bytes

I also found out that if I delete/update multiple entries in a row and then click the fab and the camera opens the amount of bytes multiply. For the following number to be reached I deleted 5 listItems in a row:

TransactionTooLargeException: data parcel size 23309336 bytes

I just wonder what type of data that is and why does it stack up for every navigation between my updateFragment and listFragment(as even when the code for deleting and updating is removed that still happens)? And why does it then become a problem when I start the camera or close the app per homebutton? Maybe contrary to my original believe the bitmap being display in the listItems and the updateFragment are somehow the problem. I display high quality bitmaps by using a fileprovider because in the addFragment and the updateFragment the imageViews are rather big and i use the same bitmap for the small imageViews on the listItems.

If anyone has any idea why that might happen I would be super gratefull!

CodePudding user response:

Author here: Found an answer to my problem even though I still don't completely get it.

Instead of using a @TypeConverter to turn a bitmap into a byteArray on storing it in the database and on retrieving turning it form byteArray back to bitmap i now store a uri as a string. I found that code which turns a bitmap into a uri and stores the bitmap on the device and it works like a charm:

private fun saveImageToInternalStorage(bitmap: Bitmap): Uri {
    val wrapper = ContextWrapper(applicationContext)
    var file = wrapper.getDir(IMAGE_DIRECTORY, Context.MODE_PRIVATE)
    file = File(file, "${UUID.randomUUID()}.jpg")
    try{
        val stream : OutputStream = FileOutputStream(file)
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
        stream.flush()
        stream.close()
    }catch (e: IOException){
        e.printStackTrace()
    }
    return Uri.parse(file.absolutePath)
}

companion object{
        private const val IMAGE_DIRECTORY = "...Images"
    }
  • Related