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

Mauro Mazzucco
Mauro Mazzucco
933 Points

Directory is not being created

mkdirs is returning false everytime

  private Uri getOutPutMediaFileUri(int mediaTypeImage) {
            if(isExternalAvailable()){
                File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Snapshot");

                if(!mediaStorageDir.exists()){
                    if(!mediaStorageDir.mkdirs()){
                        Log.e("Dir", "Failed to create directory");
                        Log.d("MAKE DIR", mediaStorageDir.mkdir() + "" +  mediaStorageDir.getParentFile() + "");
                        return null;
                    }
                }
                File mediaFile;
                Date date = new Date();
                String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(date);
                String path = mediaStorageDir.getPath() + File.separator;

                if(mediaTypeImage == 1){
                    mediaFile = new File(path + "IMG_" + timestamp + ".jpg");
                }else if(mediaTypeImage == 2){
                    mediaFile = new File(path + "VID_" + timestamp + ".mp4");
                }else{
                    return null;
                }
                return Uri.fromFile(mediaFile);

            }else{
                return null;
            }

        }
Harry James
Harry James
14,780 Points

Hey Mauro!

Could you please add this line under the mediaStorageDir variable declaration:

Log.d("MainActivity", "Media storage dir is: " + mediaStorageDir.getPath());

Then paste the result from Logcat on this post. This will help me to debug your problem :)

2 Answers

Mauro Mazzucco
Mauro Mazzucco
933 Points

Here:

10-02 14:25:03.480 16637-16637/com.snapshot D/MainActivity﹕ Media storage dir is: /storage/emulated/0/Pictures/Snapshot

Harry James
Harry James
14,780 Points

Hey Mauro!

That looks right to me! Go ahead and plug my debug code in after the log statement that we Failed to create directory:

Log.d("MainActivity", ">> Let's debug why this directory isn't being created: ");
Log.d("MainActivity", "Is it working?: " + mediaStorageDir.mkdirs());
Log.d("MainActivity", "Does it exist?: " + mediaStorageDir.exists());
Log.d("MainActivity", "What is the full URI?: " + mediaStorageDir.toURI());
Log.d("MainActivity", "--");
Log.d("MainActivity", "Can we write to this file?: " + mediaStorageDir.canWrite());
if (!mediaStorageDir.canWrite()) {
    Log.d("MainActivity", ">> We can't write! Do we have WRITE_EXTERNAL_STORAGE permission?");
    if (getBaseContext().checkCallingOrSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE") == PackageManager.PERMISSION_DENIED) {
        Log.d("MainActivity", ">> We don't have permission to write - please add it.");
    } else {
        Log.d("MainActivity", "We do have permission - the problem lies elsewhere.");
    }
}
Log.d("MainActivity", "Are we even allowed to read this file?: " + mediaStorageDir.canRead());
Log.d("MainActivity", "--");
Log.d("MainActivity", ">> End of debugging.");

Send back the results and I'll look into it! :)

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;
                }
            };
Peng Ouyang
Peng Ouyang
5,805 Points

Hello Harry,

I also have the same problem and I tried your debug method. Here is the result: D/MainActivity: Media storage dir is: /storage/emulated/0/Android/data/com.teamtreehouse.cameraworkshop/files/DCIM 12-19 23:11:53.298 23716-23716/com.teamtreehouse.cameraworkshop D/MainActivity: >> Let's debug why this directory isn't being created: 12-19 23:11:53.298 23716-23716/com.teamtreehouse.cameraworkshop D/MainActivity: Is it working?: false 12-19 23:11:53.298 23716-23716/com.teamtreehouse.cameraworkshop D/MainActivity: Does it exist?: true 12-19 23:11:53.298 23716-23716/com.teamtreehouse.cameraworkshop D/MainActivity: What is the full URI?: file:/storage/emulated/0/Android/data/com.teamtreehouse.cameraworkshop/files/DCIM/ 12-19 23:11:53.298 23716-23716/com.teamtreehouse.cameraworkshop D/MainActivity: -- 12-19 23:11:53.298 23716-23716/com.teamtreehouse.cameraworkshop D/MainActivity: Can we write to this file?: true 12-19 23:11:53.298 23716-23716/com.teamtreehouse.cameraworkshop D/MainActivity: Are we even allowed to read this file?: true 12-19 23:11:53.298 23716-23716/com.teamtreehouse.cameraworkshop D/MainActivity: -- 12-19 23:11:53.298 23716-23716/com.teamtreehouse.cameraworkshop D/MainActivity: >> End of debugging.