1

我想从我的手机摄像头中单击图像,然后将此图像保存在存储在 sdcard.plz 中的 sqlite 数据库中,告诉我我该怎么做?以下是我为单击图像编写的代码

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = new File(Environment.getExternalStorageDirectory(), bb);

        outputFileUri = Uri.fromFile(file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, TAKE_PICTURE);
4

1 回答 1

1

在清单中授予权限:

<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 类型

于 2013-07-10T08:09:26.973 回答