如何将图像插入sqlite数据库
How to insert an Image to sqlite database
每当我尝试将图像添加到 sqlite table 它显示 null 尽管单词已成功插入。
代码:
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri imageUri = data.getData();
try {
Bitmap bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG,100,bos);
byte[] img=bos.toByteArray();
ContentValues cv=new ContentValues();
cv.put("image", img);
db.insertImage(cv,editword,editcategory);
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
数据库方案:
`String sql = "Insert Into Images (Images, Name, Category) Values ("+cv+", '"+name+"', '"+category+"')";
mDb.execSQL(sql);`
table 中的图像数据类型是 Blob
我通过放置解决了这个问题:
Bitmap bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
单独出现在 try-catch 子句中。我还替换了:
db.insertImage(cv,editword,editcategory);
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
在新函数中
每当我尝试将图像添加到 sqlite table 它显示 null 尽管单词已成功插入。
代码:
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri imageUri = data.getData();
try {
Bitmap bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG,100,bos);
byte[] img=bos.toByteArray();
ContentValues cv=new ContentValues();
cv.put("image", img);
db.insertImage(cv,editword,editcategory);
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
数据库方案:
`String sql = "Insert Into Images (Images, Name, Category) Values ("+cv+", '"+name+"', '"+category+"')";
mDb.execSQL(sql);`
table 中的图像数据类型是 Blob
我通过放置解决了这个问题:
Bitmap bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
单独出现在 try-catch 子句中。我还替换了:
db.insertImage(cv,editword,editcategory);
Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show();
在新函数中