Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Capturing Photos and Videos Setting Where Photos are Saved

how do i add makestorageDir??

im adding thi directory but it cannot locate that method

Can you be more specific ? you problably used the wrong method with "auto correct" or could be a typo

1 Answer

Andre Colares
Andre Colares
5,437 Points

Here is the functional code (ANDROID STUDIO 1.4):

protected DialogInterface.OnClickListener mDialogListener =
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    switch (which){
                        case 0: //Take Picture
                            Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            // get path to save images
                            mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
                                //display an error
                            }else {
                                takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);

                                startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST);

                        case 1: //Take Video

                        case 2: //Choose Picture

                        case 3: //Choose Video
                private Uri getOutputMediaFileUri(int mediaType) {
                    //To be safe, you should check that the SDCard is mounted
                    //using Environment.getExternalStorageState() before doing this

                        //Get the URI

                        //1. Get the external storage directory
                        String appName = MainActivity.this.getString(R.string.app_name);
                        File mediaStorageDir = new File(

                        // create subdirectory
                        if ( ! mediaStorageDir.exists() ) {
                            if ( ! mediaStorageDir.mkdirs() ) {
                                Log.e(TAG, "Failed to create directory");
                                return null;
                        //3.Create a file name
                        File mediaFile;

                        //4. Create the file
                        Date now = new Date();
                        String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(now);

                        String path = mediaStorageDir.getPath() + File.separator;

                        switch (mediaType) {
                            case MEDIA_TYPE_IMAGE:
                                mediaFile = new File(path + "IMG_" + timestamp + ".jpg");
                            case MEDIA_TYPE_VIDEO:
                                mediaFile = new File(path + "VID_" + timestamp + ".mp4");
                                return null;

                        Log.d(TAG, "File: " + Uri.fromFile(mediaFile));

                        //5. Return the file's Uri
                        return Uri.fromFile(mediaFile);
                    }else {
                        return null;

                private boolean isExternalStorageAvaible(){
                    String state = Environment.getExternalStorageState();
                    return state.equals(Environment.MEDIA_MOUNTED) ? true : false;