【问题标题】:Android trying to put image as a ByteArray into sqlite database, getting error on put methodAndroid试图将图像作为ByteArray放入sqlite数据库,put方法出错
【发布时间】:2012-04-27 07:12:28
【问题描述】:

我正在尝试将图像与一些字符串一起输入数据库,但我的 put 方法出现以下错误:

The method put(String, String) in the type ContentValues is not applicable for the arguments (String, Byte[]) 

但是当我在教程中看到它时,它似乎和我的做法完全一样。

以下是相关代码:

private static final String DATABASE_CREATE = 
    "CREATE Table " + DATABASE_TABLE_LIST + " ( "
    + LIST_ID + " integer PRIMARY KEY Autoincrement, "
    + LIST_NAME + " text NOT NULL, "
    + LIST_ADDRESS + " text NOT NULL, " 
    + LIST_IMAGE + " BLOB );";

...

public long createItem(String name, String address, Byte[] image) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(LIST_NAME, name);
    initialValues.put(LIST_ADDRESS, address);
    initialValues.put(LIST_IMAGE, image);
    return mDb.insert(DATABASE_TABLE_LIST, null, initialValues);
}

我猜这可能与放置字符串和字节数组有关,但我不知道如何解决它,也无法通过谷歌搜索找到它

【问题讨论】:

    标签: android image sqlite bytearray put


    【解决方案1】:

    您尝试插入 B yte 数组对我来说似乎很奇怪。您的图像数据很可能是一个 b yte 数组。由于 Byte 对象数组没有 put 方法,而只有原始数据类型 byte 的数组,所以编译器不知道该做什么。

    【讨论】:

      【解决方案2】:

      看这里:

      http://www.tutorialforandroid.com/2009/10/how-to-insert-image-data-to-sqlite.html

      DefaultHttpClient mHttpClient = new DefaultHttpClient();  
      HttpGet mHttpGet = new HttpGet("your image url");  
      HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);  
      if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
        HttpEntity entity = mHttpResponse.getEntity();  
          if ( entity != null) {  
            // insert to database  
            ContentValues values = new ContentValues();  
            values.put(MyBaseColumn.MyTable.ImageField, EntityUtils.toByteArray(entity));  
            getContentResolver().insert(MyBaseColumn.MyTable.CONTENT_URI, values);  
          }  
      }  
      

      【讨论】:

      • 是的,这是我用作基础的教程之一。但我的查询和方法在单独的 DatabaseAdapter 类中与数据库交互。我在我的活动类中调用 createItem 方法,因此我不必在我想使用它的每个不同的活动类中重写那些数据库交互代码块。在我的活动类中,我会调用类似 DatabaseAdapter.createItem(String名称,字符串地址,字节[]图像);
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 2020-03-24
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 2011-07-22
      相关资源
      最近更新 更多