Android Database access image

Source: Internet
Author: User

There are two ways to access images in an Android database, one is to save the path to the image, and the other is to store the image in binary form (Sqlite3 supports BLOB data types). For the use of the two methods, as if the second method than the first method is more popular with programmers, they think, in many database languages, the processing of large segments is not easy, such as pictures of files in the database will have problems: Read and write to the database is never the speed of the file system, so that the database becomes huge But there are also many people who think that data like images are also good for storing in databases: Easy backup, faster backups than backup files, easier data migration, and more. In fact, both of these methods have advantages and disadvantages, the specific use of which method depends on the situation. Individuals tend to use the database to access the image, because the individual believes that the data stored in the database will not be lost due to changes in external data, such as you take pictures to obtain a picture, if the path is stored in the database, when the photo is deleted, the next time you read the database will not get the desired results. The following is a detailed description of how the database accesses images:

1. Get the Bitmap object from the resource

1     Resources res = getresources ();  2     Bitmap BMP = Bitmapfactory.decoderesource (res, r.drawable.icon); 

2. Convert images to bytes

Byte[] img (int id){new bytearrayoutputstream ();  4 Bitmap Bitmap = ((bitmapdrawable) getresources (). getdrawable (ID)). getbitmap ();  5 bitmap.compress (Bitmap.CompressFormat.PNG,BAOs);  return Baos.tobytearray ();  7}           

3. Insert a picture in the database

//When the database is created, the data type of the picture field is stored as a BLOB database insert operationPublicvoid OnCreate (Sqlitedatabase db) {String sql =" CREATE TABLE "+ Tb_name +" ("+ ID +" in Teger primary KEY, "+ IMAGE +" BLOB) "; Db.execsql (SQL);} // store the database read operation in one byte of the picture long Insert (byte [] img) {sqlitedatabase db = Getwritabledatabase (); Contentvalues CV = new Contentvalues (); Cv.put (IMAGE, IMG); long result = Db.insert (tb_name, null, CV); return      

4. Get the picture stored in the database (BITMAP)

Public Bitmap getbmp (int position) {    Sqlitedatabase db = getreadabledatabase ();    cursor cursor = Select (tb_name);    Cursor.movetoposition (position);    byte[] in = Cursor.getblob (cursor.getcolumnindex (IMAGE)); Bitmap bmpout = Bitmapfactory.decodebytearray (in, 0return bmpout;}       

Imgview.setimagebitmap (BM);

5. Convert the acquired picture (Bitmap) to drawable

Public drawable chage_to_drawable (Bitmap BP){// because Btimapdrawable is a subclass of drawable and ultimately directly uses the BD object. 4 Bitmap bm=newreturn BD;  7}        

Android database access picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.