您可以通过这种方式将相机捕获的图像保存到 SQLite 数据库中。
公共类 CamActivity 扩展 Activity {
byte[] byteArray;
private static final int CAMERA_REQUEST = 1888;
protected static final int TAKE_PHOTO_CODE = 0;
public ImageView imageView;
private DBAdapter db;
byte [] imgbyte;
EditText txtView ;
String name;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db=new DBAdapter(CamActivity.this);
db.open();
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
txtView=(EditText)findViewById(R.id.editText1);
Button B = (Button) this.findViewById(R.id.camera);
B.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,getImageUri());
startActivityForResult(cameraIntent,CAMERA_REQUEST );
}
});
Button save = (Button)findViewById(R.id.saving);
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
name=txtView.getText().toString();
try
{
db.insertImageDetails(byteArray,name);
}
catch (Exception e) {
e.printStackTrace();
}
//mySQLiteAdapter.close();
Toast.makeText(getApplicationContext(), "processing", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "image saved", Toast.LENGTH_SHORT).show();
}});
Button G = (Button) this.findViewById(R.id.get);
G.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent intent= new Intent(CamActivity.this,SecondActivity.class);
startActivity(intent);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//final byte[] byteArray;
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
//imageView.setImageBitmap(photo);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
System.out.println(byteArray);
Toast.makeText(getApplicationContext(), byteArray.toString(), Toast.LENGTH_SHORT).show();
}
}
}