我是安卓新手。我创建了一个应用程序,用于使用 android 手机拍照并将该照片保存在 Sqlite 数据库中。但是这段代码没有运行。我无法在程序中找到错误。请帮我。
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 0;
public ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView =(ImageView) findViewById(R.id.image);
Button B = (Button) this.findViewById(R.id.camera);
B.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ImageHelper help= new ImageHelper(this);
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
ByteArrayOutputStream out=new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG,100,out);
byte ByteArr[]=out.toByteArray();
help.insert(ByteArr);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
数据库代码
public class ImageHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="image";
private static final int DATABASE_VERSION=1;
public ImageHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE Image(_id INTEGER PRIMARY KEY AUTOINCREMENT,imageblob BLOB);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public Cursor getAll() {
return(getReadableDatabase().rawQuery("SELECT imageblob FROM Image",null));
}
public void insert(byte Byte[]){
ContentValues cv=new ContentValues();
cv.put("imageblob",Byte);
Log.e("inserted","inserted");
getWritableDatabase().insert("image","imageblob",cv);
}
public byte[] getImage(Cursor c)
{
return(c.getBlob(1));
}
}