Question

In my Android application, I want to capture an image from the camera and pass that image Uri to next activity.

Here, I am getting a thumbnail image of captured image. To solve that issue, I used MediaStore.EXTRA_OUTPUT for my picture capture intent.

Now, I don't know how to get the uri of currently captured image. I tried the code below and passing imageUri in next activity, but it throws NoSuchFileDirectory exception.

I referred this link, but are not getting any clear idea about getting the captured image Uri and passing it to next activity. Please help me to solve this issue. If you find any problem with this code then suggest where I am going wrong.

Code:

        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;
    }
    }
Was it helpful?

Solution

I used this code and its working for me to save camera image and getting uri of that image.

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;
}
    }

OTHER TIPS

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

here your creating subdirectory and a file in one shot. you create directory then image file

update try this

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

remove this from oncick listener

         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();

            }

I created a wrapper to help you with that. See the whole discussion on stackoverflow here or find the code on github.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top