我正在为客户创建一个应用程序来存储他们的图像和细节。我正在通过相机或 SD 卡浏览图像。我ImageView
通过 sdcard 获取图像,但是当我将其插入数据库时,它没有插入数据库的图像列中。
我单击 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();
}
} }
}
现在,当我单击保存按钮时,其他数据存储在数据库中,但图像列在数据库中为空,我没有收到任何错误。
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
图像存储在数据库中。