How can I get the image path of an image taken with the camera?


December 2018


3.2k time


in my app you can click on a image view which gives you the option to chose an image from the sd card or take a new picture with the camera. I already implemented the option to chose an image from the sd card. Now I am left with the camera option.

My question: How can I get the image path of any new image taken with the camera?

Below you find the code I use to open up the the Dialog window and the way I get the path of images from the sd card.

1.This is the code for the dialog:

private void showImageDialog() {
    final String [] items = new String [] {"From Camera", "From SD Card"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Select Image");
    builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
        public void onClick( DialogInterface dialog, int item ) {
            if (item == 0) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File file = new File(Environment.getExternalStorageDirectory(),
                                    "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
                mImageCaptureUri = Uri.fromFile(file);

                try {
                    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
                    intent.putExtra("return-data", true);

                    startActivityForResult(intent, PICK_FROM_CAMERA);
                } catch (Exception e) {

            } else {
                Intent intent = new Intent();


                startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
    } );

    final AlertDialog dialog = builder.create();;

2.And this is what is use to get the image path from the sd card image.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == PICK_FROM_FILE || requestCode == PICK_FROM_CAMERA) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);

            Log.v("IMAGE PATH====>>>> ",selectedImagePath);

            // Decode, scale and set the image.
            Bitmap myBitmap = BitmapFactory.decodeFile(selectedImagePath);
            Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, NEW_WIDTH, NEW_HEIGHT, true);
            myBitmap = null;



0 answers