2012-01-05 63 views
1

我正在开发一个应用程序,它在sqlite中存储一些静态图像,我想在imageview中检索相同的图像。 我该怎么做 谢谢。我怎么能存储图像和检索相同的sqlite-android

+0

在SD卡上存储图像,使用SD卡的路径并将其存储在数据库中。稍后,当您需要图像时,只需使用此路径检索图像并在图像视图中显示。 – 2012-01-05 08:24:05

+0

我想将图像作为blob存储在数据库中。我不想使用文件路径。 – user1002448 2012-01-05 09:40:06

回答

7

sqlite3支持blob类型,您可以使用blob类型保存位图内容。

但是,blob类型具有大小限制,并且保存为blob类型很困难。

所以,我建议保存位图本地或SD卡上,并将路径保存在数据库中。

加入

表定义一个名为“图像”使用blob类型的列

Bitmap map = ...; 
    ByteArrayOutputStream bufferStream = new ByteArrayOutputStream(16*1024); 
    map.compress(CompressFormat.JPEG, 80, bufferStream); 
    byte[] bytes = bufferStream.toByteArray(); 
    ContentValues values = new ContentValues(); 
    values.put("image", bytes); 

转换图像字节数组,使用SQLiteDatabase类方法或内容提供者,只要你喜欢:

public long insert (String table, String nullColumnHack, ContentValues values)

插入表格,所以它保存图像到blob

然后:在查询blob数据,你可以这样创建图像:

 BitmapFactory.Options option2 = new BitmapFactory.Options(); 
     option2.inPreferredConfig = Bitmap.Config.RGB_565; 
     // added for reducing the memory 
     option2.inDither = false; 
     option2.inPurgeable = true; 
     return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, option2); 

希望它可以实现你的要求。 - ):

+0

我想要存储确切的图像,而不是路径。我知道我需要使用blob。但我没有得到如何实现它。 – user1002448 2012-01-05 09:41:15

+0

真棒,完美的作品,谢谢!关于将图像存储在SD卡上:用户可能会移动/删除它。而某些设备可能没有SD卡开始。 – Buffalo 2012-10-03 14:28:10