如何将图片转换存入到数据库中,并从数据库中取出转换成图片
有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来。
首先,我们要把图片存入到数据库中,首先要创建一个数据库, 如下所示:
1 package com.android.test;
2
3 import java.io.ByteArrayOutputStream;
4
5 import android.content.ContentResolver;
6 import android.content.ContentValues;
7 import android.content.Context;
8 import android.database.sqlite.SQLiteDatabase;
9 import android.database.sqlite.SQLiteOpenHelper;
10 import android.graphics.Bitmap;
11 import android.graphics.Bitmap.CompressFormat;
12 import android.graphics.drawable.BitmapDrawable;
13 import android.graphics.drawable.Drawable;
14 import android.provider.BaseColumns;
15
16 public class PictureDatabase extends SQLiteOpenHelper {
17
18 //数据库的字段
19 public static class PictureColumns implements BaseColumns {
20 public static final String PICTURE = "picture";
21 }
22
23 private Context mContext;
24
25 //数据库名
26 private static final String DATABASE_NAME = "picture.db";
27 //数据库版本号
28 private static final int DATABASE_Version = 1;
29 //表名
30 private static final String TABLE_NAME = "picture";
31
32 //创建数据库
33 public PictureDatabase (Context context) {
34 super(context, DATABASE_NAME, null, DATABASE_Version);
35 this.mContext = context;
36 }
37
38 //创建表并初始化表
39 @Override
40 public void onCreate (SQLiteDatabase db) {
41 String sql = "Create table " + TABLE_NAME + "(" + BaseColumns._ID
42 + " integer primary key autoincrement," + PictureColumns.PICTURE
43 + " blob not null);";
44 db.execSQL(sql);
45
46 //初始化
47 initDataBase(db,mContext);
48 }
49
50 //将转换后的图片存入到数据库中
51 private void initDataBase (SQLiteDatabase db, Context context) {
52 Drawable drawable = context.getResources().getDrawable(R.drawable.test_icon_resizer);
53 ContentValues cv = new ContentValues();
54 cv.put(PictureColumns.PICTURE, getPicture(drawable));
55 db.insert(TABLE_NAME, null, cv);
56 }
57
58 //将drawable转换成可以用来存储的byte[]类型
59 private byte[] getPicture(Drawable drawable) {
60 if(drawable == null) {
61 return null;
62 }
63 BitmapDrawable bd = (BitmapDrawable) drawable;
64 Bitmap bitmap = bd.getBitmap();
65 ByteArrayOutputStream os = new ByteArrayOutputStream();
66 bitmap.compress(CompressFormat.PNG, 100, os);
67 return os.toByteArray();
68 }
69
70 //更新数据库
71 @Override
72 public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
73 String sql = " DROP TABLE IF EXISTS " + TABLE_NAME;
74 db.execSQL(sql);
75 onCreate(db);
76 }
77 }
代码注释的比较详细.
这里重点要说的是初始化数据库的时候,将Drawable转变成byte[]的时候,先讲Drawable转换成Bitmap,然后将Bitmap存入字节数据输出流,从输出流里获取byte[]数组。
ByteArrayOutputStream os = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, os);
return os.toByteArray();
之后将字符数组存入到类型为blob的数据库中去。
ContentValues cv = new ContentValues();
cv.put(PictureColumns.PICTURE, getPicture(drawable));
db.insert(TABLE_NAME, null, cv);
之后在代码中从数据库中取出byte[],然后转换成Drawable,设置图片即可。
代码如下:
1 package com.android.test;
2
3 import java.util.ArrayList;
4
5 import android.app.Activity;
6 import android.database.Cursor;
7 import android.database.sqlite.SQLiteDatabase;
8 import android.graphics.Bitmap;
9 import android.graphics.BitmapFactory;
10 import android.graphics.drawable.BitmapDrawable;
11 import android.graphics.drawable.Drawable;
12 import android.os.Bundle;
13 import android.widget.ImageView;
14
15 public class TestPicture extends Activity {
16
17 @Override
18 protected void onCreate (Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 ImageView iv = new ImageView(this);
21 if(getDrawable().size() != 0) {
22 iv.setImageDrawable(getDrawable().get(0));
23 }
24 setContentView(iv);
25 }
26
27
28 private ArrayList<Drawable> getDrawable() {
29 PictureDatabase pd = new PictureDatabase(this);
30 SQLiteDatabase sd = pd.getWritableDatabase();
31
32 ArrayList<Drawable> drawables = new ArrayList<Drawable>();
33
34 //查询数据库
35 Cursor c = sd.query("picture", null, null, null, null, null, null);
36
37 //遍历数据
38 if(c != null && c.getCount() != 0) {
39 while(c.moveToNext()) {
40 //获取数据
41 byte[] b = c.getBlob(c.getColumnIndexOrThrow(PictureDatabase.PictureColumns.PICTURE));
42 //将获取的数据转换成drawable
43 Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length, null);
44 BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
45 Drawable drawable = bitmapDrawable;
46 drawables.add(drawable);
47 }
48 }
49 return drawables;
50 }
51 }
重点注意如何将数据库中取出的byte[]转换成drawable:
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length, null);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
Drawable drawable = bitmapDrawable;
运行效果如下: