我正在使用SQLite数据库来存储“餐馆”。我正在尝试添加一个具有其属性的餐馆,但是当我尝试将图像路径从SD卡存储到SQLite数据库时,我得到一个运行时异常。
这是代码。
examinar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
agregar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String nombre1 = String.valueOf(nombre.getText());
String tipo1 = tipo;
String direccion1 = String.valueOf(direccion.getText());
String descripcion1 = String.valueOf(descripcion.getText());
Toast.makeText(getApplicationContext(),
"ruta : +" + imagen1, Toast.LENGTH_SHORT)
.show();
if (nombre1.length() != 0 && tipo1.length() != 0 && direccion1.length() != 0 && descripcion1.length() != 0 && imagen1.length() != 0) {
dataSource.insertarRestaurant1(nombre1, tipo1, direccion1, imagen1, descripcion1);
setResult(RESULT_OK);
finish();
Toast.makeText(getApplicationContext(),
"Insertado exitosamente", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(getApplicationContext(),
"Debe llenar todos los campos", Toast.LENGTH_SHORT)
.show();
}
}
});
cancelar.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
nombre.setText(" ");
//tipo.setText(" ");
direccion.setText(" ");
descripcion.setText(" ");
//imagen.setText(" ");
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
String filePath = selectedImage.getEncodedPath();
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imagen1 = filePath;
cursor.close();
Toast.makeText(getApplicationContext(),
"ruta : +" + filePath, Toast.LENGTH_SHORT)
.show();
}
}
}
答案 0 :(得分:0)
在ActivityForResult方法中添加以下行:
Uri selectedImageURI = data.getData();
imageFile = new File(getRealPathFromURI(selectedImageURI));
其中imageFile是File的全局对象。
然后使用下面的代码将商店图像存入数据库:
String pathimg = imageFile.getAbsolutePath();
dba.open();
dba.insertPhoto(pathimg);
其中dba是数据库类的对象。
在数据库类中创建表格,如:
private static final String CREATETABLE_PHOTO = "create table eqpphoto("EImage text " + ");";
public static final String TABLE_PHOTO = "eqpphoto";
public long insertPhoto(String EImage) {
try {
System.out.println("Function call : ");
ContentValues values = new ContentValues();
values.put(EIMAGE, EImage);
return db.insert(TABLE_PHOTO, null, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}