数据库存取图片
2015-11-27 11:26
295 查看
数据库中存取图片通常使用两种方式,一种是保存图片所在路径,二是将图片以二进制的形式存储(sqlite3支持BLOB数据类型)。对于两种方法的使用,好像第二种方法不如第一种方法更受程序员欢迎,他们认为,在很多数据库语言里,处理大字段都是不容易的,像图片这样的文件放在数据库里会有问题:对数据库的读写速度永远赶不上文件系统的处理速度,使数据库变得巨大;但也有很多人认为像图片这样的数据存放在数据库中也有好处:易于备份,且备份速度绝对比备份文件快,比较容易数据迁移等等。其实这两种方法都有优缺点,具体使用哪种方法要视情况而定。个人倾向于使用数据库存取图片,因为个人认为存到数据库里的数据不会因外部数据的变化而丢失改变,比如你拍照获得一张图片,如果是将路径存到数据库,当这张照片被删除之后,下次读取数据库就得不到想要的结果了。接下来详细介绍数据库存取图片的方法:
一、数据库创建和存取方法:
在数据库创建时,图片字段的数据类型存储为 BLOB数据库插入操作
public void onCreate(SQLiteDatabase db)
{
String sql = "create table " + TB_NAME + " ( " + ID + " integer primary key , " + IMAGE + " BLOB ) ";
db.execSQL(sql);
}
将图片一字节形式存储数据库读取操作
public long insert(byte[] img)
{
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(IMAGE, img);
long result = db.insert(TB_NAME, null, cv);
return result;
}
得到的图片是Bitmap类型,参数position 点击图片在ListView、GridView等内的位置 ,可根据需要编写代码二
public Bitmap getBmp(int position)
{
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = select(TB_NAME);
cursor.moveToPosition(position);
byte[] in = cursor.getBlob(cursor.getColumnIndex(IMAGE));
Bitmap bmpout = BitmapFactory.decodeByteArray(in, 0, in.length);
return bmpout;
}
二、将图片转化为 byte[]//参数id为图片资源 (R.drawable.img)
public byte[] img(int id)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(id)).getBitmap();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
一、数据库创建和存取方法:
在数据库创建时,图片字段的数据类型存储为 BLOB数据库插入操作
public void onCreate(SQLiteDatabase db)
{
String sql = "create table " + TB_NAME + " ( " + ID + " integer primary key , " + IMAGE + " BLOB ) ";
db.execSQL(sql);
}
将图片一字节形式存储数据库读取操作
public long insert(byte[] img)
{
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(IMAGE, img);
long result = db.insert(TB_NAME, null, cv);
return result;
}
得到的图片是Bitmap类型,参数position 点击图片在ListView、GridView等内的位置 ,可根据需要编写代码二
public Bitmap getBmp(int position)
{
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = select(TB_NAME);
cursor.moveToPosition(position);
byte[] in = cursor.getBlob(cursor.getColumnIndex(IMAGE));
Bitmap bmpout = BitmapFactory.decodeByteArray(in, 0, in.length);
return bmpout;
}
二、将图片转化为 byte[]//参数id为图片资源 (R.drawable.img)
public byte[] img(int id)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(id)).getBitmap();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
相关文章推荐
- oracle 权限管理
- unix时间通过mysql转换出来
- java连接oracle数据库,自己封装的几个方法
- hibernate单向N-1无表连接级联
- 安装MongoDB
- 8天学通MongoDB——第一天 基础入门
- sql 连接字符串大全
- 深入理解Oracle的imp/exp 和各版本之间的规则
- MySql索引算法原理解析(通俗易懂,只讲B-tree)
- HTML select 下拉菜单赋值(从数据库中)
- 数据库 'XXXX' 已打开,并且一次只能有一个用户访问。设置单用户 / 多用户
- sql: update from
- Mysql分库分表方案
- mysql bin-log日志过大造成磁盘爆满问题解决。
- mysql杀掉睡眠进程
- mysql常用优化参数
- 数据库坏块,怎么办?
- db2数据库常用调试检测数据库语句
- Oracle12C安装:Could not initialize class sun.awt.X11.XToolkit
- oracle数据库关闭和启动命令