3

在我的 Android 应用程序中,我想从相机捕获图像并将该图像传递Uri给下一个活动。

在这里,我得到了捕获图像的缩略图。为了解决这个问题,我使用MediaStore.EXTRA_OUTPUT了我的图片捕捉意图。

现在,我不知道如何获取当前捕获图像的 uri。我尝试了下面的代码并传入imageUri下一个活动,但它引发了NoSuchFileDirectory异常。

我提到了这个链接,但对获取捕获的图像 Uri 并将其传递给下一个活动没有任何清晰的想法。请帮我解决这个问题。如果您发现此代码有任何问题,请建议我哪里出错了。

代码:

        btnCamera.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent pictureActionIntent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                File photo = null;
                try {

                    photo = SaveCameraImage();

                } catch (Exception e) {
                    Log.e("Camera capture-------------",
                            "Can't create file to take picture!");
                    Toast.makeText(SelectFrameActivity.this,
                            "Please check SD card! Image shot is impossible!",
                            10000).show();

                }
                mImageUri = Uri.fromFile(photo);
                pictureActionIntent
                        .putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);

                startActivityForResult(pictureActionIntent, TAKE_PICTURE);
            }
        });


private File SaveCameraImage() {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
            String dt = sdf.format(new Date());

            imageFile = null;
            imageFile = new File(Environment.getExternalStorageDirectory(),
                    "FrameFace/" + "Camera_" + dt + ".png");
            Log.e("New Camera Image Path:-",
                    Environment.getExternalStorageDirectory() + "/FrameFace/"
                            + "Camera_" + dt + ".png");

            if (!imageFile.exists())
                imageFile.createNewFile();

            bmp_camera_image.compress(Bitmap.CompressFormat.PNG, 100,
                    new FileOutputStream(imageFile));

            Log.e("imagetext", "the file has saved to the sdcard");

            imagePath = Environment.getExternalStorageDirectory()
                    + "/FrameFace/" + "Camera_" + dt + ".png";

            Log.e("FaceFrameActivity - SaveCameraImage: ", "Image is Saved to"
                    + imagePath);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return imageFile;
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode,
            final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case TAKE_PICTURE:
            Log.e("result code: ", Integer.toString(Activity.RESULT_OK) + " "
                    + Integer.toString(resultCode));

            if (resultCode == Activity.RESULT_OK) {

                // bmp_camera_image = (Bitmap) data.getExtras().get("data");
                SaveCameraImage();

                // this.getContentResolver().notifyChange(mImageUri, null);
                // ContentResolver cr = this.getContentResolver();
                // try {
                // GlobalClass.cameraBitmap =
                // android.provider.MediaStore.Images.Media
                // .getBitmap(cr, mImageUri);
                // } catch (FileNotFoundException e) {
                // // TODO Auto-generated catch block
                // e.printStackTrace();
                // } catch (IOException e) {
                // // TODO Auto-generated catch block
                // e.printStackTrace();
                // }

                Handler h = new Handler();
                h.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        Intent intent = new Intent(getApplicationContext(),
                                FaceCropActivity_dual.class);
                        // intent.putExtra("imagePath", imagePath);
                        intent.putExtra("imagePath", mImageUri.toString());
                        startActivity(intent);
                    }
                }, 500);
            }
            break;
    }
    }
4

3 回答 3

2

我使用此代码及其对我的工作来保存相机图像并获取该图像的 uri。

btnCamera.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    dialog.dismiss();
                    SimpleDateFormat sdf = new SimpleDateFormat(
                            "yyyyMMdd_HHmmss");
                    String dt = sdf.format(new Date());

                    imageFile = null;
                    imageFile = new File(Environment
                            .getExternalStorageDirectory() + "/FrameFace/",
                            "Camera_" + dt + ".png");
                    Log.e("New Camera Image Path:-",
                            Environment.getExternalStorageDirectory()
                                    + "/FrameFace/" + "Camera_" + dt + ".png");

                    if (!imageFile.exists())
                        imageFile.createNewFile();

                    imagePath = Environment.getExternalStorageDirectory()
                            + "/FrameFace/" + "Camera_" + dt + ".png";

                    Uri outputFileUri = Uri.fromFile(imageFile);

                    Intent pictureActionIntent = new Intent(
                            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                    pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            outputFileUri);

                    startActivityForResult(pictureActionIntent, TAKE_PICTURE);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
@Override
    protected void onActivityResult(int requestCode, int resultCode,
            final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case TAKE_PICTURE:
            Log.e("result code: ", Integer.toString(Activity.RESULT_OK) + " "
                    + Integer.toString(resultCode));

            if (resultCode == Activity.RESULT_OK) {
Handler h = new Handler();
                h.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        Intent intent = new Intent(getApplicationContext(),
                                FaceCropActivity_dual.class);
                        // intent.putExtra("imagePath", imagePath);
                        Log.e("Image Path-------------", "" + imagePath);
                        intent.putExtra("imagePath", imagePath);
                        startActivity(intent);
                    }
                }, 500);
            }
            break;
}
    }
于 2014-01-06T11:36:11.310 回答
0
imageFile = new File(Environment.getExternalStorageDirectory(),
                "FrameFace/" + "Camera_" + dt + ".png");

在这里,您可以一次性创建子目录和文件。您创建目录,然后创建图像文件

更新 试试这个

 imageFile = new File(Environment.getExternalStorageDirectory()+"/FrameFace" ,"Camera_" + dt + ".png");

从 oncick 监听器中删除它

         File photo = null;
            try {

                photo = SaveCameraImage();

            } catch (Exception e) {
                Log.e("Camera capture-------------",
                        "Can't create file to take picture!");
                Toast.makeText(SelectFrameActivity.this,
                        "Please check SD card! Image shot is impossible!",
                        10000).show();

            }
于 2014-01-06T07:39:43.190 回答
0

我创建了一个包装器来帮助你。在此处查看有关 stackoverflow 的完整讨论或在github上查找代码。

于 2014-02-02T10:58:55.543 回答