Whenever I try to add an Image from gallery and store it in sqlite database it shows me the following error:
unrecognized token: "[B@4a9a0f, 'ثعلب', 'Animals')" (code 1): , while compiling: Insert Into Images (Images, Name, Category) Values (null=[B@4a9a0f, 'ثعلب', 'Animals')
I want to store the image as Blob.
Here is the full code:
`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();
}`
why does ContentValue add the name tag to the byte[] value and how to remove null=[ from the byte[] value
Your issue is that you are providing null as a column name by using cv.put(null,bte);
So you should be using cv.put("Images",bte);
However, I suspect that you are also not using ContentValues as expected subsequently but are trying to build the SQL rather than using the convenience SQLiteDatabase insert method, which handles the ContentValues and builds the SQL on your behalf. The example uses this method.
Here's a very simple example of inserting (with the image as hard coded byte[]):-
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);
}
}
The first works as expected, the second (insertImageWrong) fails with :-
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)
The following (snapshot from Android Studio's Database Inspector) shows that the first attempt inserted the row into the database:-
I would suggest that you adapt the insertImage method as above and use that.
WARNING
Trying to store images in the database can be problematic. If the images are small (100-200kb) then fine. However, any larger and you may well experience issues with the speed. If the images are greater than 1Mb then you may well experience crashes. If any image is greater them 4Mb then you will undoubtedly experience crashes unless you split the image into chunks.