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;
运行效果如下:
上一篇: Android自定义桌面功能代码实现
下一篇: CAD怎么不改变尺寸缩放图形?