Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

mMediaUri is always null, even on the emulator

mMediaUri seems like it doesn't get anything. I always get to the toast message from if mMediaUri is null. Any suggestions to this? I even tried copying and pasting the code from the project and it doesn't change anything. Also is not the permissions because I have all of them

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-feature android:name="android.hardware.camera" android:required="true"/>

here is my MainAcitvity.java http://pastebin.com/pvQuVAkF

Have you made sure your emulator has emulated sd storage?

2 Answers

Afik Paryente I am not really sure how to do that but I have tried running the app on my phone and table and still get the same result

Daniel Hair
Daniel Hair
5,080 Points

What does it say in your logcat when you run your emulator? Does it every say that you cannot create a directory? (I misspelled WRITE_EXTERNAL_STORAGE)

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;