从相机捕获

时间:2017-08-15 09:41:48

标签: android

在调试以下代码时,我将picUri的值设为null,我可以在跟踪中看到picUri=null data:"Intent" {act=inline-data (has extras)}"。为什么picUri没有相应的uri并且有额外的数据?

public void onClick(View v) {
        if (v.getId() == R.id.capture_btn) {
            try {
                // use standard intent to capture an image
                Intent captureIntent = new Intent(
                        MediaStore.ACTION_IMAGE_CAPTURE);
                // we will handle the returned data in onActivityResult
                startActivityForResult(captureIntent, CAMERA_CAPTURE);
            } catch (ActivityNotFoundException anfe) {
                Toast toast = Toast.makeText(this, "This device doesn't support the crop action!",
                        Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == CAMERA_CAPTURE) {
                // get the Uri for the captured image
                picUri = data.getData();
                                        }
                 }
}

2 个答案:

答案 0 :(得分:3)

我已收到Bundleonactivityresult的数据并将其存储到file,并从uri获得file,如下所示。< / p>

    Bundle extras = data.getExtras();
    Bitmap var_Bitmap = (Bitmap) extras.get("data");
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    var_Bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] bytes = stream.toByteArray();

try {

    OutputStream out;
    String root = Environment.getExternalStorageDirectory().getAbsolutePath()+"/";
    File createDir = new File(root+"macro"+File.separator);
    createDir.mkdir();


    File file = new File(root + "macro" + File.separator +"macro.jpg");

    file.createNewFile();
    out = new FileOutputStream(file);
    out.write(bytes);
    out.close();

    picUri= Uri.fromFile(file);
} catch (IOException e) {
    // e.printStackTrace();
}

答案 1 :(得分:0)

  1. 在此传递相机意图

    private void INTENTCAMERA() {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_REQUEST);
    }
    
    1. 之后拍摄了你的照片
    2. protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
      if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
          Bitmap photo = (Bitmap) data.getExtras().get("data"); 
          imageView.setImageBitmap(photo);
          knop.setVisibility(Button.VISIBLE);
      
      
          // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
          Uri tempUri = getImageUri(getApplicationContext(), photo);
      
          // CALL THIS METHOD TO GET THE ACTUAL PATH
          File finalFile = new File(getRealPathFromURI(tempUri));
      
          Log.e("ResultcapturedImage-->",mImageCaptureUri);
      }  
       }
      
      public Uri getImageUri(Context inContext, Bitmap inImage) {
      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
      inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
      String path = Images.Media.insertImage(inContext.getContentResolver(), 
      inImage, "Title", null);
      return Uri.parse(path);
      }
      
      public String getRealPathFromURI(Uri uri) {
      Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
      cursor.moveToFirst(); 
      int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
      return cursor.getString(idx); 
      }