Android尝试将图像作为ByteArray放入sqlite数据库,在put方法上出错

yvt65v4c  于 2023-04-21  发布在  SQLite
关注(0)|答案(2)|浏览(131)

我试图将图像和一些字符串一起输入数据库,但我在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);
}

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

sg24os4d

sg24os4d1#

我觉得很奇怪,你试图插入一个Byte数组。你的图像数据很可能是一个byte数组。由于没有针对Byte对象数组的put方法,而只有针对原始数据类型 byte 的数组,编译器不知道该怎么做。

kognpnkq

kognpnkq2#

看这里:
https://web.archive.org/web/20200718180158/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);  
    }  
}

相关问题