安卓中从数据库取出特定的数据图书后如何转换成png

用手机怎么把jpg图片转换为png图片?_百度知道  本来打算用数据库sqlite存取图片(图片都是相机拍摄的原图),结果导致存入和读取的时候会消耗巨大内存,尤其是从数据库取图片时。所以准备存SDCard代替,但还是记录下如何用数据库存取图片以及转换成缩略图。
  表结构一个String和一个Blob。bitmap不能直接存数据库,用BLOB&(binary large object)二进制大对象。
String sql = "create table team (name varchar(20) primary key, image blob);";
  bitmap先要转换成二进制数组。
public byte[] img(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pressFormat.PNG, 100, baos);
return baos.toByteArray();
  插入数据库。(person.getImage()就是一个bitmap)
public void insert(Person person) {
SQLiteDatabase db = openHelper.getWritableDatabase();
if (db.isOpen()) {
db.execSQL("insert into team(name,image) values (?, ?);",
new Object[]{person.getName(), img(person.getImage())});
db.close();
  接下来取图片和转换成缩略图。
  BitmapFactory.decodeByteArray(in, 0, in.length)可以把取出来的二进制数组in转换成bitmap。
  BitmapFactory.Options options = new BitmapFactory.Options();  //解析位图的附加条件
  options.inJustDecodeBounds =  //inJustDecodeBounds设为true,不去解析真实的位图,读取头文件获取基本信息
  options.inSampleSize  //位图缩放比例,好像实际取值只能是2的幂次方(15取8,17取16,未考证)
  最后inJustDecodeBounds = false,接下来就加载缩略图。
public ArrayList&Person& queryAll() {
SQLiteDatabase db = openHelper.getReadableDatabase();
if (db.isOpen()) {
Cursor cursor = db.rawQuery("select *", null);
if (cursor != null && cursor.getCount() & 0) {
ArrayList&Person& teamList = new ArrayList&Person&();
while (cursor.moveToNext()) {
name = cursor.getString(0);
byte[] in = cursor.getBlob(1);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
image = BitmapFactory.decodeByteArray(in, 0, in.length, options);
int bitmapWidth = options.outW
int bitmapHeight = options.outH
int x = 180;
int dx = bitmapWidth /
int dy = bitmapHeight /
if (dx & dy && dy & 1) {
options.inSampleSize =
if (dy & dx && dx & 1) {
options.inSampleSize =
options.inJustDecodeBounds = false;
image = BitmapFactory.decodeByteArray(in, 0, in.length, options);
teamList.add(new Person(name, image));
db.close();
return teamL
db.close();
return null;
return null;
阅读(...) 评论()怎么将一张普通的png图片转换层bitmap_百度知道<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&

我要回帖

更多关于 数据库取出特定的数据 的文章

 

随机推荐