android将图片转换存到数据库再从数据库读取转换成图片实现代码
首先,我们要把图片存入到数据库中,首先要创建一个数据库, 如下所示:
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 {
//数据库的字段
public static class PictureColumns implements BaseColumns {
public static final String PICTURE = "picture";
}
private Context mContext;
//数据库名
private static final String DATABASE_NAME = "picture.db";
//数据库版本号
private static final int DATABASE_Version = 1;
//表名
private static final String TABLE_NAME = "picture";
//创建数据库
public PictureDatabase (Context context) {
super(context, DATABASE_NAME, null, DATABASE_Version);
this.mContext = context;
}
//创建表并初始化表
@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);
//初始化
initDataBase(db,mContext);
}
//将转换后的图片存入到数据库中
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);
}
//将drawable转换成可以用来存储的byte[]类型
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, 100, os);
return os.toByteArray();
}
//更新数据库
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = " DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
}
代码注释的比较详细.
这里重点要说的是初始化数据库的时候,将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,设置图片即可。
代码如下:
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>();
//查询数据库
Cursor c = sd.query("picture", null, null, null, null, null, null);
//遍历数据
if(c != null && c.getCount() != 0) {
while(c.moveToNext()) {
//获取数据
byte[] b = c.getBlob(c.getColumnIndexOrThrow(PictureDatabase.PictureColumns.PICTURE));
//将获取的数据转换成drawable
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length, null);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
Drawable drawable = bitmapDrawable;
drawables.add(drawable);
}
}
return drawables;
}
}
重点注意如何将数据库中取出的byte[]转换成drawable:
代码如下:
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length, null);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
Drawable drawable = bitmapDrawable;
运行效果如下: