首先,我们要把图片存入到数据库中,首先要创建一个数据库, 如下所示:
复制代码 代码如下:
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,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,os);
return os.toByteArray();
之后将字符数组存入到类型为blob的数据库中去。
复制代码 代码如下:
ContentValues cv = new ContentValues();
cv.put(PictureColumns.PICTURE,getPicture(drawable));
db.insert(table_name,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);
//遍历数据
if(c != null && c.getCount() != 0) {
while(c.movetoNext()) {
//获取数据
byte[] b = c.getBlob(c.getColumnIndexOrThrow(PictureDatabase.PictureColumns.PICTURE));
//将获取的数据转换成drawable
Bitmap bitmap = BitmapFactory.decodeByteArray(b,b.length,null);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
Drawable drawable = bitmapDrawable;
drawables.add(drawable);
}
}
return drawables;
}
}
重点注意如何将数据库中取出的byte[]转换成drawable:
复制代码 代码如下:@H_419_190@
Bitmap bitmap = BitmapFactory.decodeByteArray(b,null);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
Drawable drawable = bitmapDrawable;
运行效果如下:
以上是内存溢出为你收集整理的android将图片转换存到数据库再从数据库读取转换成图片实现代码全部内容,希望文章能够帮你解决android将图片转换存到数据库再从数据库读取转换成图片实现代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)