Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像

Android学习 之 Bitmap Drawable byte[] 三者之间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像

1.创建数据库表的时候选择存图片的字段类型为blob

 

Java代码 Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像
  1. StringBuffer createTableOfHistory = new StringBuffer();
  2. createTableOfHistory.append("CREATE TABLE "+某表名);
  3. createTableOfHistory.append(" ( _id INTEGER PRIMARY KEY AUTOINCREMENT ,");
  4. createTableOfHistory.append(该存图片的字段名+" BLOB ,");
  5. createTableOfHistory.append(其他字段名1+" TEXT ,");
  6. .......
  7. createTableOfHistory.append(其他字段名n+" TEXT );");//记得这里带个“;”封号
  8. 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将数据流转成数组的方法

Java代码 Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像
  1. InputStream inputStream = getResources().openRawResource(R.drawable.icon);
  2. private static byte[] streamToBytes(InputStream is) {
  3. ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
  4. byte[] buffer = new byte[1024];
  5. int len;
  6. try {
  7. while ((len = is.read(buffer)) >= 0) {
  8. os.write(buffer, 0, len);
  9. }
  10. } catch (java.io.IOException e) {
  11. }
  12. return os.toByteArray();
  13. }
 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】

Java代码 Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像
  1. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
  2. private static byte[] bitmapToBytes(Bitmap bitmap){
  3. if (bitmap == null) {
  4. return null;
  5. }
  6. final ByteArrayOutputStream os = new ByteArrayOutputStream();
  7. // 将Bitmap压缩成PNG编码,质量为100%存储
  8. bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);//除了PNG还有很多常见格式,如jpeg等。
  9. return os.toByteArray();
  10. }
  11. ContentValues values = new ContentValues();
  12. values.put(该存图片的字段名, readHistoryInfo.getBookIcon());
  13. values.put(其他字段名1, “2011-05-17”);
  14. ......
  15. 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()也是该类的一个打开或创建数据库的方法。

 

Java代码 Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像
  1. Cursor cursor = 某类对象m.getData(该存图片的字段名, null);
  2. if (cursor.moveToFirst()) {
  3. // byte[] —> Bitmap
  4. byte[] bytes = cursor.getBlob(cursor.getColumnIndex(该存图片的字段名));
  5. Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);
  6. BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
  7. }
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

Java代码 Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像
  1. Bitmap bm = xxx; //xxx根据你的情况获取
  2. BitmapDrawable bd= new BitmapDrawable(bm);
  3. 提示:因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
Bitmap bm = xxx; //xxx根据你的情况获取
  BitmapDrawable bd= new BitmapDrawable(bm); 

  提示:因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。

 

 

5. Drawable —> Bitmap

 

Java代码 Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像
  1. public static Bitmap drawableToBitmap(Drawable drawable) {
  2. Bitmap bitmap = Bitmap.createBitmap(
  3. drawable.getIntrinsicWidth(),
  4. drawable.getIntrinsicHeight(),
  5. drawable.getOpacity() != PixelFormat.OPAQUE ?
  6. Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);
  7. return bitmap;
  8. }
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

 

Java代码 Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像Android学习 之 Bitmap Drawable byte[] 三者其间的转换以及把数组存入数据库及提取数据重新组合成所需对象,如图像
  1. Drawable d=xxx; //xxx根据自己的情况获取drawable
  2. BitmapDrawable bd = (BitmapDrawable) d;
  3. Bitmap bm = bd.getBitmap();
  Drawable d=xxx; //xxx根据自己的情况获取drawable
  BitmapDrawable bd = (BitmapDrawable) d;
  Bitmap bm = bd.getBitmap();