There are two ways to access images in Android databases. One is to save the path where the image is located, and the other is to store the image in binary form (sqlite3 supports BLOB data types). For the use of the two methods, it seems that the second method is not as popular as the first method. They believe that in many database languages, it is not easy to process large fields. There will be problems with files like pictures placed in the database: the reading and writing speed of the database can never keep up with the processing speed of the file system, making the database huge; but many people also think that storing data like pictures is also beneficial in the database: it is easy to backup, and the backup speed is definitely faster than backup files, which is easier to migrate data, etc. In fact, both methods have advantages and disadvantages. Which method is used depends on the situation. Individuals tend to use database to access pictures because they believe that the data stored in the database will not be lost and changed due to changes in external data. For example, if you take a photo and get an image, if you save the path to the database, when the photo is deleted, you will not get the desired result the next time you read the database. Next, we will introduce in detail the method of database accessing pictures:
1. Convert the picture to bytes
private byte[]img(Bitmap bitmap){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); (, 100, baos); return (); }
2. Store the pictures in the database
Assuming the obtained image is bitmap, the database has a User table, and the stored attribute is byte[]headshot
public class User extends DataSupport { private byte[] headshot;//avatar public User(){ super(); } public User(byte[]headshot){ super(); =headshot; } public byte[] getHeadshot() { return headshot; } public void setHeadshot(byte[] headshot) { = headshot; } }
Save the picture
//Get the pictureBitmap headShot=(imagePath); //Convert the image to byte streambyte[]images=img(headShot); //Finding userUser users=(); //save(images); ();
4. Get pictures
User mUser=(); byte[]images=(); Bitmap bitmap=(images,0,);
OK, here we complete the database access image.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.