Android SQLite access images and convert thumbnails

Source: Internet
Author: User

Originally intended to use the database SQLite access to images (Pictures are taken from the camera), resulting in the storage and reading will consume huge memory, especially from the database to take pictures. So prepare to save SDcard instead, but still record how to use the database to access the image and convert to thumbnail image.

The table structure is a string and a blob. Bitmap cannot directly store the database, using a BLOB (binary large object) binary large object.

String sql = "CREATE TABLE team (name varchar () primary key, image blob);";

Bitmap is converted to a binary array first.

 Public byte [] img (Bitmap Bitmap) {        new  bytearrayoutputstream ();         , BAOs);         return Baos.tobytearray ();    }

Insert the database. (Person.getimage () is a bitmap)

 Public void Insert (person person) {        = openhelper.getwritabledatabase ();         if (Db.isopen ()) {            db.execsql ("INSERT INTO Team (Name,image) VALUES (?,?);" ,                     New object[]{person.getname (), IMG (Person.getimage ())});            Db.close ();        }    }

Next take the picture and convert it into thumbnails.

Bitmapfactory.decodebytearray (in, 0, in.length) can convert the extracted binary array in to bitmap.

Bitmapfactory.options Options = new Bitmapfactory.options (); Additional conditions for parsing bitmaps

Options.injustdecodebounds = true; Injustdecodebounds set to True, do not parse the real bitmap, read the header file for basic information

Options.insamplesize//Bitmap scaling, as if the actual value can only be 2 power (15 take 8, 17 take 16, not verified)

Finally Injustdecodebounds = False, then the thumbnail is loaded.

 PublicArraylist<person>Queryall () {Sqlitedatabase db=openhelper.getreadabledatabase (); if(Db.isopen ()) {cursor cursor= Db.rawquery ("SELECT * from Team;",NULL); if(Cursor! =NULL&& cursor.getcount () > 0) {ArrayList<Person> teamlist =NewArraylist<person>();                String name;                Bitmap image;  while(Cursor.movetonext ()) {name= cursor.getstring (0); byte[] in = Cursor.getblob (1); Bitmapfactory.options Options=Newbitmapfactory.options (); Options.injustdecodebounds=true; Image= Bitmapfactory.decodebytearray (in, 0, in.length, Options); intBitmapwidth =Options.outwidth; intBitmapheight =Options.outheight; intx = 180; intDX = bitmapwidth/x; intDY = bitmapheight/x; if(dx > Dy && dy > 1) {options.insamplesize=DX; }                    if(Dy > dx && dx > 1) {options.insamplesize=dy; } options.injustdecodebounds=false; Image= Bitmapfactory.decodebytearray (in, 0, in.length, Options); Teamlist.add (NewPerson (name, image));                } db.close (); returnteamlist;            } db.close (); return NULL; }        return NULL; }

Android SQLite access images and convert thumbnails

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.