- 浏览: 37029 次
- 性别:
文章分类
最新评论
1.创建数据库表的时候选择存图片的字段类型为blob
- StringBuffer createTableOfHistory = new StringBuffer();
- createTableOfHistory.append("CREATE TABLE "+某表名);
- createTableOfHistory.append(" ( _id INTEGER PRIMARY KEY AUTOINCREMENT ,");
- createTableOfHistory.append(该存图片的字段名+" BLOB ,");
- createTableOfHistory.append(其他字段名1+" TEXT ,");
- .......
- createTableOfHistory.append(其他字段名n+" TEXT );");//记得这里带个“;”封号
- db.execSQL(createTableOfHistory.toString());//执行该创表语句
StringBuffer createTableOfHistory = new StringBuffer(); createTableOfHistory.append("CREATE TABLE "+某表名); createTableOfHistory.append(" ( _id INTEGER PRIMARY KEY AUTOINCREMENT ,"); createTableOfHistory.append(该存图片的字段名+" BLOB ,"); createTableOfHistory.append(其他字段名1+" TEXT ,"); ....... createTableOfHistory.append(其他字段名n+" TEXT );");//记得这里带个“;”封号 db.execSQL(createTableOfHistory.toString());//执行该创表语句
2.存储数据
2.1将数据流转成数组的方法
- InputStream inputStream = getResources().openRawResource(R.drawable.icon);
- private static byte[] streamToBytes(InputStream is) {
- ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
- byte[] buffer = new byte[1024];
- int len;
- try {
- while ((len = is.read(buffer)) >= 0) {
- os.write(buffer, 0, len);
- }
- } catch (java.io.IOException e) {
- }
- return os.toByteArray();
- }
InputStream inputStream = getResources().openRawResource(R.drawable.icon); private static byte[] streamToBytes(InputStream is) { ByteArrayOutputStream os = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int len; try { while ((len = is.read(buffer)) >= 0) { os.write(buffer, 0, len); } } catch (java.io.IOException e) { } return os.toByteArray(); }
2.2.将Bitmap对象转换成数组的方法【包含从资源文件中获得图片对象Bitmap】
- Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
- private static byte[] bitmapToBytes(Bitmap bitmap){
- if (bitmap == null) {
- return null;
- }
- final ByteArrayOutputStream os = new ByteArrayOutputStream();
- // 将Bitmap压缩成PNG编码,质量为100%存储
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);//除了PNG还有很多常见格式,如jpeg等。
- return os.toByteArray();
- }
- ContentValues values = new ContentValues();
- values.put(该存图片的字段名, readHistoryInfo.getBookIcon());
- values.put(其他字段名1, “2011-05-17”);
- ......
- return mSqliteDatabase.insert(表名, null, values);//插入数据
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon); private static byte[] bitmapToBytes(Bitmap bitmap){ if (bitmap == null) { return null; } final ByteArrayOutputStream os = new ByteArrayOutputStream(); // 将Bitmap压缩成PNG编码,质量为100%存储 bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);//除了PNG还有很多常见格式,如jpeg等。 return os.toByteArray(); } ContentValues values = new ContentValues(); values.put(该存图片的字段名, readHistoryInfo.getBookIcon()); values.put(其他字段名1, “2011-05-17”); ...... return mSqliteDatabase.insert(表名, null, values);//插入数据
3.提取数据库中的数组数据并转换成Bitmap或DrawableBitmap对象【包含byte[] —> Bitmap】
某类对象m(该类是负责创表,删表,插入数据,删除数据的类).openOrCreateDB();//openOrCreateDB()也是该类的一个打开或创建数据库的方法。
- Cursor cursor = 某类对象m.getData(该存图片的字段名, null);
- if (cursor.moveToFirst()) {
- // byte[] —> Bitmap
- byte[] bytes = cursor.getBlob(cursor.getColumnIndex(该存图片的字段名));
- Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);
- BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
- }
Cursor cursor = 某类对象m.getData(该存图片的字段名, null); if (cursor.moveToFirst()) { // byte[] —> Bitmap byte[] bytes = cursor.getBlob(cursor.getColumnIndex(该存图片的字段名)); Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null); BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap); }
4.Drawable —> Bitmap
- Bitmap bm = xxx; //xxx根据你的情况获取
- BitmapDrawable bd= new BitmapDrawable(bm);
- 提示:因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
Bitmap bm = xxx; //xxx根据你的情况获取 BitmapDrawable bd= new BitmapDrawable(bm); 提示:因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
5. Drawable —> Bitmap
- public static Bitmap drawableToBitmap(Drawable drawable) {
- Bitmap bitmap = Bitmap.createBitmap(
- drawable.getIntrinsicWidth(),
- drawable.getIntrinsicHeight(),
- drawable.getOpacity() != PixelFormat.OPAQUE ?
- Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);
- return bitmap;
- }
public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565); return bitmap; }
or
- Drawable d=xxx; //xxx根据自己的情况获取drawable
- BitmapDrawable bd = (BitmapDrawable) d;
- Bitmap bm = bd.getBitmap();
Drawable d=xxx; //xxx根据自己的情况获取drawable BitmapDrawable bd = (BitmapDrawable) d; Bitmap bm = bd.getBitmap();
发表评论
-
MediaPlayer与SoundPool小结
2013-07-23 15:05 572http://blog.csdn.net/j67065/art ... -
为什么要将thread对象post到handler中执行呢?
2013-07-23 13:28 723http://www.cnblogs.com/sipher/a ... -
Android APK反编译详解(附图)
2012-10-11 14:19 854这段时间在学Android应用开发,在想既然是用Java开发的 ... -
Android Layout之三:RalativeLayout(一)
2012-05-09 10:28 1我们从RalativeLayout可以 ... -
android
2012-05-08 15:43 0afdsfsdafdsf -
android SDK升级连接不上服务器解决方案(SDK版本无法下载)
2012-04-06 13:24 916我离线安装ADT1.7,结果ADT版本太高,无法下载升级S ... -
android中的显示单位
2012-04-06 10:50 6291、px(像素) 一般HVGA代表320*480像素,这个用的 ... -
Android相对布局
2011-12-14 16:40 1172RelativeLayout布局 android: ... -
Android布局中ScrollView与ListView的冲突
2012-05-07 14:23 1394亲~本店毛衣质量上乘,由厂家直接进货,色泽明亮、手感丰满,价格 ... -
图片宽度尺寸输出为500px的PX
2011-12-01 11:05 1606亲~本店毛衣质量上乘,由厂家直接进货,色泽明亮、手感丰满,价格 ... -
android: layout_alignParentRight android_paddingRight
2011-12-01 11:03 1102亲~本店毛衣质量上乘,由厂家直接进货,色泽明亮、手感丰满,价格 ... -
android:paddingLeft与android:layout_marginLeft的区别
2011-12-01 10:58 1486亲~本店毛衣质量上乘,由厂家直接进货,色泽明亮、手感丰满,价格 ... -
Android 中颜色对应的代码集合
2011-12-01 10:55 908亲~本店毛衣质量上乘,由厂家直接进货,色泽明亮、手感丰满,价格 ... -
Android 五大布局
2011-12-01 10:52 646http://shop69104873.taobao.com/ ...
相关推荐
Android Drawable、Bitmap、byte、灰度 之间的转换
Android Drawable、Bitmap、byte、灰度 转换
详细的介绍了android_Drawable、Bitmap、byte[]之间的转换 在android开发中非常实用
android bitmap通过byte数组以流的形式创建,自己加了进度条,看起来稍微好点
一个在VC中将Bitmap转换为Byte[]的小例子。
Drawable Bitmap InputStream byte[]相互转化工具类,单例模式
文档中描述了Drawable Bitmap 几种转换方法供初学者使用。方法都是经过实践验证的。
将Bitmap转成byte[],进行保存,用到时再取出byte[],转化成Bitmap,进行显示
android 画图 bitmap drawable canvas paint
Drawable Bitmap之间的转化Drawable Bitmap之间的转化
android平台jpeg数据流转换成rgb直接显示的so库 使用方式: byte[] jpegRaw = V4L2Camera.decode(dataBuf, ret); rgb2Buffer(jpegRaw, WIDTH, HEIGHT, rgbBuffer); ByteBuffer jpegBuffer = ByteBuffer.wrap(rgb...
Android Drawable和Bitmap的转换实例详解 通常我们需要通过代码去设置图片,就需要设置图片Bitmap和Drawable的转换,下面整理了几种方式 一、Bitmap转Drawable Bitmap bm=xxx; //xxx根据你的情况获取 ...
Android图片Bitmap和字符串String之间相互转换的工具类,外加项目代码,封装好的,直接可以用,
将图片由bitmap类型转换为stream类型再转换为byte[]类型,然后再还原
2、把从数据库读取的byte数组转换为Image对象,赋值给相应的控件显示。 3、从图片byte数组得到对应图片的格式,生成一张图片保存到磁盘上。 这里的Image是System.Drawing.Image。 //Get an image from file Image...
很多开发者表示,不知道Android的Drawable和Bitmap之间如何相关转换。下面Android123给大家两种比较简单高效的方法。 一、Bitmap转Drawable 代码如下: Bitmap bm=xxx; //xxx根据你的情况获取 BitmapDrawable bd...
Android不支持将Bitmap转换成单色的Bmp图片,所以参考Bmp格式说明,自己写了一个转换类。亲测有效!!!
Halcon_Bitmap转换方法 写的文档 开发笔记
解决方法之一:把Bitmap对象替换成byte数组来表示间接表示图片,在需要Bitmap的时候再讲byte数组转换成Bitmap对象。这是因为byte数组和Bitmap之间的可以转化,实现也比较方便。 附byte数组与Bitmap的相互转换方法: ...