SQLite 数据库无法识别 ContentValue android studio 的值

SQLite db doesn't recognize the value of ContentValue android studio

每当我尝试从图库中添加图像并将其存储在 sqlite 数据库中时,它会显示以下错误:
unrecognized token: "[B@4a9a0f, 'ثعلب', 'Animals')" (code 1): , while compiling: Insert Into Images (Images, Name, Category) Values (null=[B@4a9a0f, 'ثعلب', 'Animals')

我想将图像存储为 Blob。

完整代码如下:

       `if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
        Uri imageUri = data.getData();
        try {
            bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        tst.setImageBitmap(bmp);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG,100,bos);
        bte=bos.toByteArray();
        cv.put(null,bte);
    }

}

public void AddWord(View view) {
    editword = edtwrd.getText().toString();
    editcategory = edtcat.getText().toString();
    //db.insertword(editword, editcategory);
    db.insertImage(cv,editword,editcategory);
    Toast.makeText(getApplicationContext(), "Data is added successfully", Toast.LENGTH_LONG).show();
}`

ContentValue 为什么要给 byte[] 值加上 name 标签以及如何从 byte[] 值中去掉 null=[

您的问题是您使用 cv.put(null,bte);

提供 null 作为列名

所以你应该使用 cv.put("Images",bte);

但是,我怀疑您随后也没有按预期使用 ContentValues,而是试图构建 SQL 而不是使用便利 SQLiteDatabase insert方法,它代表您处理 ContentValues 并构建 SQL。该示例使用此方法。

这是一个非常简单的插入示例(图像作为硬编码字节[]):-

public class MainActivity extends AppCompatActivity {

    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = (new MyDatabase(this)).getWritableDatabase();

        byte[] bte = new byte[]{0,1,2,3,4,5,6,7};
        insertImage(bte,"ثعلب","Animals");
        insertImageWrong(bte,"ثعلب","Animals");
    }

    long insertImage(byte[] image, String name, String category) {
        ContentValues cv = new ContentValues();
        cv.put("Images",image);
        cv.put("Name",name);
        cv.put("Category",category);
        return db.insert("Images" /* The Table Name */, null, cv /* The content values with the column name value pairs */);
    }

    long insertImageWrong(byte[] image, String name, String category) {
        ContentValues cv = new ContentValues();
        cv.put(null,image);
        cv.put("Name",name);
        cv.put("Category",category);
        return db.insert("Images",null,cv);
    }
}

第一个按预期工作,第二个 (insertImageWrong) 失败:-

2021-07-04 07:19:55.099 9266-9266/a.a.so68239336nullcontentvalues E/SQLiteLog: (1) near "null": syntax error
2021-07-04 07:19:55.101 9266-9266/a.a.so68239336nullcontentvalues E/SQLiteDatabase: Error inserting null=[B@be55dc1 Category=Animals Name=ثعلب
    android.database.sqlite.SQLiteException: near "null": syntax error (code 1 SQLITE_ERROR): , while compiling: INSERT INTO Images(null,Category,Name) VALUES (?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1562)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1433)
        at a.a.so68239336nullcontentvalues.MainActivity.insertImageWrong(MainActivity.java:37)
        at a.a.so68239336nullcontentvalues.MainActivity.onCreate(MainActivity.java:21)

以下 (来自 Android Studio 的数据库检查器的快照) 显示第一次尝试将行插入数据库:-

我建议您像上面那样调整 insertImage 方法并使用它。

警告

尝试将图像存储在数据库中可能会出现问题。如果图像很小(100-200kb),那么没问题。然而,任何更大的,你可能会遇到速度问题。如果图像大于 1Mb,那么您很可能会遇到崩溃。如果任何图像大于 4Mb,那么除非将图像分成块,否则您无疑会遇到崩溃。