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