【问题标题】:Store and retreieve blob from sqllite database for android从 android 的 sqlite 数据库中存储和检索 blob
【发布时间】:2012-07-26 03:56:04
【问题描述】:

我需要知道在 Eclipse 中定义 blob 数据类型。我正在为 android 开发一个应用程序,我想将图像和记录保存在数据库中。我知道必须使用 blob 作为数据类型。我的问题是如何在类和 DBhandler 类中定义它。有人可以提供代码方面的帮助吗?

【问题讨论】:

    标签: android database eclipse image recording


    【解决方案1】:

    Blob 用于在 sqlite 中保存字节数组。

    要将位图转换为字节数组,请使用以下代码:

    Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.thumbnail);
    
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    
    image.compress(Bitmap.CompressFormat.PNG, 100, out);
    
    byte[] buffer=out.toByteArray();
    

    要将字节数组保存为 blob 类型,请使用以下代码:

       ContentValues cv=new ContentValues();
       cv.put(CHUNK, buffer);       //CHUNK blob type field of your table
       long rawId=database.insert(TABLE, null, cv); //TABLE table name
    

    通过link了解有关 Blob 的更多信息

    【讨论】:

      猜你喜欢
      • 2011-09-09
      • 2012-09-24
      • 2011-12-30
      • 2010-10-12
      • 2015-11-21
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 1970-01-01
      相关资源
      最近更新 更多