@TypeConverter - Insert Image in Database | Android Studio Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
uh hello there and welcome back to my new video in this video i want to show you how to use a type converter and type converter allows us to store custom type objects in a room database and in this video specifically i want to show you how to store a bitmap object in a database so as you already know room database accepts only small number of types like string integer float long and so on but more complex objects are not allowed directly so basically type converter is used to convert non-acceptable type objects to acceptable ones when writing to our database and convert back to original type objects when a reading from database as well okay so back to android studio and i have already created the simple project for this purpose so here we have one activity activity main and this is how our layout looks like so we have only one view here which is a recycler view and i have already designed this row layout for my recycle view we have only three views two text views first last name and the one image view okay so basically we're going to store our bitmap object to our database and then we're going to read that bitmap from our database and display inside this image view okay so next let's open up a main activity so here i have only initialized this uh recyclerview adapter and this myviewmodel and i have already set the adapter on my recyclerview as well and down below you can see one function that says get bitmap so if you haven't watched my video about coil image library i highly suggest you to watch that before you continue because in that video i have shown you how to basically convert the image from url to a bitmap object and that's the same function from that previous video so basically this function is uh converting this uh image url to a bitmap object and we're going to use this function to pass a bitmap object to our room database okay so next we have a simple entity class person and we have two fields for now first and last name with a type of a string and of course we have a id which represents a primary key so our id is located inside this class body and we don't need to actually specify the id when we are creating a person object it will be uh automatically generated for us okay so next we have our dao class so here we are basically reading our database and we are using a live data object and down below we have a function for inserting our person object to our room database and next we have just a simple room database builder here and here we have a repository and here we have a view model so it's uh basic and simple and if you're not familiar with the room library then i highly suggest you to watch my tutorial series about room and finally we have a recyclerview adapter so it's basic and simple okay so let's open up this person class for now and let's say we want to add the bitmap object here as a field in our person class so let's add for example one well profile photo of a type bitmap okay all right so inside our main activity uh let's try and inserting this person to our room database okay so down below i'm going to first create a one person object okay so here we need to pass three parameters first name for example john last name for example do and for the profile photo we're going to get the bitmap from our get bitmap function down below so let's type get bitmap and here we have a warning telling us that this get bitmap function is a suspend function and we need to run that inside the core thing so i'm going to cut this and i'm going to add a lifecycle scope here so i can launch this block of code inside our core routine let's uh paste that here and as you can see that warning disappeared now okay so down below i'm going to use a myviewmodel and to get the reference to this insert person function so we can actually insert the person to our room database okay so now if we try to run this app we're going to get an error because uh we cannot add a bitmap object directly to our room database so let's test that out okay so there we go we received an error saying that cannot figure out how to save this field into a database and you can consider adding a type converter for it so basically uh we are not allowed to add this bitmap object directly to our database so we're going to fix that now with type converter okay and the way we're going to do that we're going to create a new class here costly class named for example converters okay and inside this class we're going to define a two functions one function which will convert our bitmap object to a byte array in the second function which will convert the byte array back to our bitmap object okay so the first function which will convert our bitmap to byte array will be used when writing to our room database in the second function which converts byte array back to a bitmap object will basically be used when reading out from our database so let's create the first function from bitmap and we're going to add the bitmap object as a parameter and we want to return byte array okay so here we're going to create the output stream byte array output stream so we're going to use a bitmap object then a method called the compress and here we're going to use the bitmap.uh compress format so we need to specify the format of our image and in this case it's a png file then the second parameter is a quality i'm going to set that to 100 and the third parameter is output stream so let's pass that here and we want to return a byte array so return output stream dot 2 byte array so basically this function will convert bitmap to byte array so we can store a byte array inside our room database and we need to annotate this function with the type converter so we have two annotations uh type converters and a type converter so we need to choose this second one and this annotation will tell our room database that whenever it recognize a bitmap object inside our entity class then it should automatically convert that to byte array okay and here we have specified a way to uh convert that bitmap to this byte array okay and down below we need to create another function and this time we need to convert the byte array to bitmap so two bitmap and here as a parameter we need to pass a byte array and we need to return a bitmap okay so here we're going to use a bitmap factory dot the code byte array the first parameter is a byte array from the parameters for the offset we're going to specify zero and for the length we're going to choose a bytearray.size and of course we need to specify a return keyword here okay and let's annotate this function with typeconverter notation so basically uh this is how we're going to tell our room database how to convert bitmap to byte array and byte array to bitmap as well so now we need to specify this converters class inside our my database okay so let's open up our database builder and here we need to annotate our class with type converters this time so the first one with s okay and here we need to specify our converter class so converters and that's all you need to do okay so basically we have specified to our room database how to convert this object and how to retrieve that object back to our application so uh inside our main activity when we try to uh insert this bitmap object it should work fine okay so there is a one more thing which we need to add in our main activity so basically we need to observe our room database and we're going to do that with live data so let's use the myviewmodel.readperson and let's observe that so okay and inside this observer we're going to set the data to our recyclerview adapter okay so let's use our my adapter for our recyclerviewadapter and let's use a function named set data and here just pass a person okay all right so now uh when we run our application uh when our oncreate method is called then we're going to immediately uh insert this person and then we're going to observe our room database and we're going to set the data to our recyclerview okay and just one more thing so we need to open up our my adapter class okay so now we need to bind our view with the data which we have passed through this set data function so let's use a holder.itemvue.imageview and i'm going to call a method called the load which is a part of a coil image library and here i'm going to pass a person position dot profile photo okay so this will automatically load our bitmap object to our imageview okay so let's run app and let's see if everything's gonna work fine okay so now as you can see we have received the same values john for the first name and doe for the last name and of course our imageview is the exact one which we have stored inside our room database so everything works perfectly fine and of course this source code will be available for you to download in the video description as well so that will be all for this video i hope you enjoyed please like this video if you find it helpful of course and see you next one oh my [Applause] when god lay me down to rest
Info
Channel: Stevdza-San
Views: 9,060
Rating: 4.9722223 out of 5
Keywords: insert, add, image, picture, bitmap, from url, to, database, room, library, how to, guide, tutorial, android studio, android, app, type converter, type, converter, type converters, annotation, @typeconverter, @typeconverters, convert, complex, object, objects, put, data, in, bytearray, byte, array, output stream, output, stream, dao, entity, repository, viewmodel, recyclerview
Id: adGU0A80EJ0
Channel Id: undefined
Length: 9min 52sec (592 seconds)
Published: Tue Oct 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.