在清单中授予权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
File destination = new File(Environment.getExternalStorageDirectory(),"image.jpg");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
startActivityForResult(intent, CAMERA_PICTURE);
然后从目标图像覆盖 onActivityResult gat 图像文件,将其转换为位图并保存。然后将其转换为字节数组
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == CAMERA_PICTURE && resultCode == Activity.RESULT_OK) {
realImageStr = new String("");
try {
FileInputStream in = new FileInputStream(destination);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 10; //Downsample 10x
Log.d("PP", " bitmap factory=========="+options);
Bitmap user_picture_bmp = BitmapFactory.decodeStream(in, null, options);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
user_picture_bmp.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] bArray = bos.toByteArray();
} catch (Exception e)
{
} }}
当您将保存为字节类型时,然后将图像列中的字节代码保存到数据库中,图像列数据类型为BLOB 类型。