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() {
                @Override
                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);
                            if(mMediaUri==null){
                                //display an error
                                Toast.makeText(MainActivity.this,
                                        R.string.error_external_storage,
                                        Toast.LENGTH_LONG).show();
                            }else {
                                takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);

                                startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST);
                            }
                            break;

                        case 1: //Take Video
                            break;

                        case 2: //Choose Picture
                            break;

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

                    if(isExternalStorageAvaible()){
                        //Get the URI

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

                        // 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");
                                break;
                            case MEDIA_TYPE_VIDEO:
                                mediaFile = new File(path + "VID_" + timestamp + ".mp4");
                                break;
                            default:
                                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;
                }
            };