zoukankan      html  css  js  c++  java
  • Android 使用SQLite存储以及读取Drawable对象

    在进行Android开发过程中,我们经常会接触到Drawable对象,那么,若要使用数据库来进行存储及读取,该如何实现?

    一、存储

    //第一步,将Drawable对象转化为Bitmap对象
    Bitmap bmp = (((BitmapDrawable)tmp.image).getBitmap());
    //第二步,声明并创建一个输出字节流对象
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    //第三步,调用compress将Bitmap对象压缩为PNG格式,第二个参数为PNG图片质量,第三个参数为接收容器,即输出字节流os
    bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
    //第四步,将输出字节流转换为字节数组,并直接进行存储数据库操作,注意,所对应的列的数据类型应该是BLOB类型
    ContentValues values = new ContentValues();
    values.put("image", os.toByteArray());
    db.insert("apps", null, values);
    db.close();

    代码看起来比较繁琐,是因为过程的确挺繁琐的,不过可以简单的总结为:

    Drawable→Bitmap→ByteArrayOutputStream→SQLite

    二、读取

    //第一步,从数据库中读取出相应数据,并保存在字节数组中  
    byte[] blob = cursor.getBlob(cursor.getColumnIndex("image"));  
    //第二步,调用BitmapFactory的解码方法decodeByteArray把字节数组转换为Bitmap对象  
    Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0, blob.length);  
    //第三步,调用BitmapDrawable构造函数生成一个BitmapDrawable对象,该对象继承Drawable对象,所以在需要处直接使用该对象即可  
    BitmapDrawable bd = new BitmapDrawable(bmp);  

    由于BitmapDrawable是Drawable的子类,所以直接使用就行了。

    很显然,读取是存储的相反过程,代码思路也差不多,但实现起来简单很多,总结思路为:

    SQLite→byte[]→Bitmap→BitmapDrawable

  • 相关阅读:
    在xp中用sc命令安装自己写的服务的具体步骤
    .net中导出excel数据不全的解决方案
    转载:20092010年中国商业智能市场分析
    xp发布多维数据集过程
    .net中导出excel时不能自动显示网格线的解决方案
    .net中访问多维数据集报错
    在xp中用DOS添加服务时,输入SC命令提示不可用的解决方案
    jenkins在奴隶机上安装dotnetsdk
    .NET Core 使用jenkins发布代码
    jenkins添加linux方法
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/7814025.html
Copyright © 2011-2022 走看看