如何拍照,保存并正确传递Android自定义相机的照片uri?

时间:2014-05-08 18:20:52

标签: java android

所以我试图在CameraActivity中拍照并将Uri传回MainActivity。

在CameraActivity.java中

@Override
    public void onPictureTaken(byte[] data, Camera camera) {
        // TODO Auto-generated method stub
        log("Picture Taken 1");
        if (data != null) {
        Intent i = new Intent();
        i.putExtra(EXTRA_CAMERA_DATA, data);
        setResult(RESULT_OK, i);
      } else {
        setResult(RESULT_CANCELED);
      }
      finish();

    }

MainActivity.java

private void addButtonListeners() {

        mBtnCamera = (Button) findViewById(R.id.btn_camera);


        mBtnCamera.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                Intent i = new Intent(MainActivity.this, CameraActivity.class);
                //getCameraInstance();

                mFileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

                i.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);

                startActivityForResult(i, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
                //startActivity(i);

            }
        });
}
/**
     * Receiving activity result method will be called after closing the camera
     * */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // if the result is capturing Image
        log("data is passed 1");
        if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {

            if (resultCode == RESULT_OK) {

                // successfully captured the image


                Bundle extras = data.getExtras();
                byte[] cameraData = extras.getByteArray(CameraActivity.EXTRA_CAMERA_DATA);
                if (cameraData != null) {
                  Bitmap mCameraBitmap = BitmapFactory.decodeByteArray(cameraData, 0, cameraData.length);

                  mImgTaken.setImageBitmap(mCameraBitmap);

                }

                //addPhotoToGallery();
                //previewCapturedPhoto();
            } else if (resultCode == RESULT_CANCELED) {
                // user cancelled Image capture

            } else {
                // failed to capture image
                Toast.makeText(getApplicationContext(),
                        "Failed to capture photo, Please take again!", Toast.LENGTH_SHORT).show();
            }
        }
    }

我从这里得到的照片只是一个缩略图,我想要一个完整尺寸的图像。但是,我认为正确的方法可能是保存一个临时文件,然后用你的意图传递uri。谁能指出我如何做的方向?提前谢谢。

1 个答案:

答案 0 :(得分:0)

我得到这样的全尺寸图片:

imgFile = new File(Environment.getExternalStorageDirectory () + "/someFolder/" + someName + ".jpg");


String fileName = imgFile.getAbsolutePath();

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(fileName)));

startActivityForResult(intent, REQUEST_FROM_CAMERA);

然后,在您的onActivityResult中,您可以按照您希望的方式处理URI

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    String filePath;
    Bitmap _bitmapPreScale = null;


    if (requestCode == REQUEST_FROM_FILE && resultCode == RESULT_OK) {
        Uri targetUri = data.getData();
            ... (do something)