How to use LitePal to operate Database in Android
This article mainly explains "how to use LitePal to operate the database in Android". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to operate the database with LitePal in Android".
1. Convert the picture to bytes
Private byte [] img (Bitmap bitmap) {ByteArrayOutputStream baos = new ByteArrayOutputStream (); bitmap.compress (Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray ();}
2. Store the pictures in the database
Suppose the acquired picture is bitmap, the database has a User table, and the stored attribute is byte [] headshot.
Public class User extends DataSupport {private byte [] headshot;// profile portrait public User () {super ();} public User (byte [] headshot) {super (); this.headshot=headshot;} public byte [] getHeadshot () {return headshot;} public void setHeadshot (byte [] headshot) {this.headshot=headshot;}}
Save the picture
/ / get the picture Bitmap headShot=BitmapFactory.decodeFile (imagePath); / / convert the picture to byte stream byte [] images=img (headShot); / / find the user User users=DataSupport.findFirst (User.class); / / Save users.setHeadshot (images); users.save ()
4. Get the picture
User mUser=DataSupport.findFrist (User.class); byte [] images=mUser.getHeadshot (); Bitmap bitmap=BitmapFactory.decodeByteArray (images,0,images.length); Thank you for reading, these are the contents of "how to use LitePal to operate the database in Android". After the study of this article, I believe you have a deeper understanding of how to use LitePal to operate the database in Android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!