Android 我正在从SD卡获取图像并将其显示在imageview上,现在我想将此图像插入数据库,但它未存储在db中
我正在为客户创建一个应用程序,用于存储他们的图像和细节。我正在通过相机或sd卡浏览图像。我通过SD卡在Android 我正在从SD卡获取图像并将其显示在imageview上,现在我想将此图像插入数据库,但它未存储在db中,android,database,image,insert,Android,Database,Image,Insert,我正在为客户创建一个应用程序,用于存储他们的图像和细节。我正在通过相机或sd卡浏览图像。我通过SD卡在ImageView中获取图像,但当我将其插入数据库时,它并没有插入数据库的image列中 我正在点击sd卡按钮浏览图像。现在,此图像将显示在ImageView中 Button btnsdcard = (Button) findViewById(R.id.custbtnimage); btnsdcard.setOnClickListener(new OnClickListener() {
ImageView
中获取图像,但当我将其插入数据库时,它并没有插入数据库的image列中
我正在点击sd卡按钮浏览图像。现在,此图像将显示在ImageView
中
Button btnsdcard = (Button) findViewById(R.id.custbtnimage);
btnsdcard.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
try {
InputStream imgstream = getContentResolver()
.openInputStream(selectedImage);
Bitmap btm = BitmapFactory.decodeStream(imgstream);
ImageView img = (ImageView) findViewById(R.id.imagecust);
img.setImageBitmap(btm);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
btm.compress(Bitmap.CompressFormat.PNG, 100, bo);
byte[] custimage = bo.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "file not fount",
Toast.LENGTH_SHORT).show();
}
} }
}
现在,当我点击save按钮或在数据库中存储WR数据,但数据库中的image列为空时,我不会得到任何错误
btnsave.setOnClickListener(new OnClickListener() {
@SuppressLint("ParserError")
public void onClick(View v) {
// TODO Auto-generated method stub
String name = custname.getText().toString();
String address = custaddress.getText().toString();
Long pincode = Long.parseLong(custpincode.getText().toString());
String city = custcity.getText().toString();
byte[] image=custimage;
Customerprofile profile = new Customerprofile();
profile.setName(name);
profile.setAddress(address);
profile.setPincode(pincode);
profile.setCity(city);
profile.setImage(image);
insertcust(profile);
}
private void insertcust(Customerprofile profile) {
// TODO Auto-generated method stub
DatabaseHelper mydbhelper = new DatabaseHelper(Myimage.this);
SQLiteDatabase db = mydbhelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DatabaseHelper.KEY_NAME, profile.getName());
values.put(DatabaseHelper.KEY_ADDRESS, profile.getAddress());
values.put(DatabaseHelper.KEY_PINCODE, profile.getPincode());
values.put(DatabaseHelper.KEY_CITY, profile.getCity());
values.put(DatabaseHelper.KEY_IMAGE, profile.getImage());
long adcid = db.insert(DatabaseHelper.DATABASE_TABLE, null,
values);
db.close();
}
});
}
有谁能帮我提供在数据库中存储我的
ImageView
图像的解决方案。您真的应该将图像存储在数据库之外的某个位置,只需将图像的路径放在数据库中即可
如果你真的想把它放到数据库中,你需要创建一个带有BLOB
类型的列来存储它(我提到这一点是因为你没有显示你的数据库结构)
然后需要将图像转换为字节数组,然后将其保存到数据库中。查看您的代码,看起来您正在做所有这些
找到你的问题。您正在使用局部变量,然后尝试在其范围之外引用它们。您有byte[]custimage=bo.toByteArray()在您的onActivityResult
中使用code>,然后尝试使用它byte[]image=custimage当代码超出范围时,在中单击
将custimage
声明为类字段,以便所有方法都可以访问它byte[]custimage
,将活动结果中的出现更改为custimage=bo.toByteArray()代码>并且它应该开始工作