使用SQLite存取图片资源

这几天刚好在整数据库的相关问题,把android的图标存储到数据库当中。

涉及到intent和bitmap对象的存储,首先说下intent。

存储intent:

在intent中有个toURI方法可以把intent转换成String类型的数据,然后可以把intent看成TEXT类型的数据存进数据库当中。转换过后的类型如下:商账追收

 #Intent;component=com.android.settings/.Settings;end

取出intent:

当要获取intent的时候,首先用Cursor的getString方法获得数据库中的值,然后调用Intent的parseUri方法把string类型转换成intent类型数据。

接下来说说数据库存取图片信息。

存储图片:bitmap 

private byte[] getIconData(Bitmap bitmap){

int size = bitmap.getWidth()*bitmap.getHeight()*4;

ByteArrayOutputStream out = new ByteArrayOutputStream(size);

try {

bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);

out.close();

} catch (IOException e) {

e.printStackTrace();

}

return out.toByteArray();

}

获取图片:

 Bitmap getIconFromCursor(Cursor c, int iconIndex) {

        byte[] data = c.getBlob(iconIndex);

        try {

            return BitmapFactory.decodeByteArray(data, 0, data.length);

        } catch (Exception e) {

            return null;

        }

    }

原文地址:https://www.cnblogs.com/sky7034/p/2065457.html