first, we're going to save the picture in the database, first to create a database , as follows:
Copy Code code as follows:
Package com.android.test;
Import Java.io.ByteArrayOutputStream;
Import Android.content.ContentResolver;
Import android.content.ContentValues;
Import Android.content.Context;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.database.sqlite.SQLiteOpenHelper;
Import Android.graphics.Bitmap;
Import Android.graphics.Bitmap.CompressFormat;
Import android.graphics.drawable.BitmapDrawable;
Import android.graphics.drawable.Drawable;
Import Android.provider.BaseColumns;
Public class Picturedatabase extends Sqliteopenhelper {
//database fields
public static class Picturecolumns implements Basecolumns {
public Static final String picture = "Picture";
}
Private Context Mcontext
//database name
private static final String database_name = "picture.db";
//Database version number
private static final int database_version = 1;
//Table name
private static final String table_name = "picture";
//CREATE DATABASE
public picturedatabase (context context) {
Super (context, database_name, NULL, database_version);
this.mcontext = context;
}
Creating tables and Initializing tables
@Override
public void OnCreate (Sqlitedatabase db) {
String sql = "Create table" + table_name + "(" + basecolumns._id
+ "Integer primary key AutoIncrement," + picturecolumns.picture
+ "blob not null);";
Db.execsql (SQL);
Class
Initdatabase (Db,mcontext);
}
Save converted pictures to the database
private void Initdatabase (Sqlitedatabase db, Context context) {
drawable drawable = Context.getresources (). getdrawable (R.drawable.test_icon_resizer);
Contentvalues CV = new Contentvalues ();
Cv.put (Picturecolumns.picture, Getpicture (drawable));
Db.insert (table_name, NULL, CV);
}
Convert drawable to byte[] types that can be stored
Private byte[] Getpicture (drawable drawable) {
if (drawable = = null) {
return null;
}
bitmapdrawable BD = (bitmapdrawable) drawable;
Bitmap Bitmap = Bd.getbitmap ();
Bytearrayoutputstream OS = new Bytearrayoutputstream ();
Bitmap.compress (compressformat.png, MB, OS);
return Os.tobytearray ();
}
Update Database
@Override
public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {
String sql = "DROP TABLE IF EXISTS" + table_name;
Db.execsql (SQL);
OnCreate (DB);
}
}
A more detailed code comment.
The emphasis here is to initialize the database when the drawable into byte[], first speak Drawable converted to bitmap, and then the bitmap into the byte data output stream, from the output stream to get the byte[] array.
Copy Code code as follows:
Bytearrayoutputstream OS = new Bytearrayoutputstream ();
Bitmap.compress (compressformat.png, MB, OS);
return Os.tobytearray ();
The character array is then stored in a database of type BLOB.
Copy Code code as follows:
Contentvalues CV = new Contentvalues ();
Cv.put (Picturecolumns.picture, Getpicture (drawable));
Db.insert (table_name, NULL, CV);
Then remove byte[from the database in the code and convert to Drawable, and set the picture.
The code is as follows:
Copy Code code as follows:
Package com.android.test;
Import java.util.ArrayList;
Import android.app.Activity;
Import Android.database.Cursor;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import android.graphics.drawable.BitmapDrawable;
Import android.graphics.drawable.Drawable;
Import Android.os.Bundle;
Import Android.widget.ImageView;
public class Testpicture extends activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
ImageView IV = new ImageView (this);
if (getdrawable (). Size ()!= 0) {
Iv.setimagedrawable (Getdrawable (). Get (0));
}
Setcontentview (iv);
}
Private Arraylist<drawable> getdrawable () {
Picturedatabase PD = new Picturedatabase (this);
Sqlitedatabase sd = Pd.getwritabledatabase ();
arraylist<drawable> drawables = new arraylist<drawable> ();
Querying the database
Cursor C = sd.query ("picture", NULL, NULL, NULL, NULL, NULL, NULL);
Traversing data
if (c!= null && c.getcount ()!= 0) {
while (C.movetonext ()) {
Get Data
Byte[] B = C.getblob (C.getcolumnindexorthrow (PictureDatabase.PictureColumns.PICTURE));
Converts the acquired data into drawable
Bitmap Bitmap = Bitmapfactory.decodebytearray (b, 0, b.length, NULL);
Bitmapdrawable bitmapdrawable = new bitmapdrawable (bitmap);
drawable drawable = bitmapdrawable;
Drawables.add (drawable);
}
}
return drawables;
}
}
Focus on how to convert the byte[from the database to drawable:
Copy Code code as follows:
Bitmap Bitmap = Bitmapfactory.decodebytearray (b, 0, b.length, NULL);
Bitmapdrawable bitmapdrawable = new bitmapdrawable (bitmap);
drawable drawable = bitmapdrawable;
The operation effect is as follows: