Android save picture to SQLite, read the picture in SQLite

Source: Internet
Author: User

1. Bitmap saved to SQLite data format:

Java code
    1. Db.execsql ("Create table Express" (_id INTEGER PRIMARY KEY autoincrement,express_no varchar), Express_name TEXT, Express_img BLOB); ");

2. Bitmap becomes Blob

Java code
    1. Contentvalues values = new Contentvalues ();
    2. Final Bytearrayoutputstream OS = new Bytearrayoutputstream ();
    3. Bmp.compress (Bitmap.CompressFormat.PNG, MB, OS);
    4. Values.put ("express_img", Os.tobytearray ());
    5. Values.put ("Express_name","ZF");
    6. Values.put ("Express_no","ZF");
    7. Getcontentresolver (). Insert ("Express", values);

3. Read bitmap from SQLite

Java code
    1. Byte[] In=cur.getblob (Cur.getcolumnindex ("express_img"));
    2. Bmpout=bitmapfactory.decodebytearray (in,0,in.length);

displayed on the ImageView

Java code
    1. ImageView ImageView = (ImageView) View.findviewbyid (r.id.img);
    2. Bytearrayinputstream stream = new Bytearrayinputstream (Cur.getblob (Cur.getcolumnindex ("express_img"));
    3. Imageview.setimagedrawable (Drawable.createfromstream (Stream, "img"));

Summarize:

InputStream: As the data cache, how the data is written for other objects to read, the method is read ();

OutputStream: As a data cache, write content to other objects in the future! Its method write ();

Java code
    1. Byte[] In=cur.getblob (Cur.getcolumnindex (MyUser.User.BITMAP_VALUES));

This also initializes the data, where byte is the base type and does not require a length definition.

Android save picture to SQLite, read the picture in SQLite

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.