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

Saving on External Storage not working

Hi there... I have a question I used this exact code by the app is saving the files on the Internal Storage, instead of the external (using an LG Spirit), I double check by clicking the properties of the image and I get this path: " / storage/emulated/0/Pictures/App Name", here's my code:

private Uri getOutputMediaFileUri(int mediaType) {
        if (isExternalStorageAvailable()) {
            //get Uri and return it
            //1.Get external storage directory
            File mediaStorageDir = new File(Environment.
                    getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "App Name");
            //2.Create a subdirectory
            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                    Log.e(TAG, "Failed to create directory");
                    return null;
                }
            }
            File mediaFile;
            Date now = new Date(); //this line and the following creates a date and a string of the specific time and place where taken the photos
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(now);
            String path = mediaStorageDir.getPath() + File.separator;
            switch (mediaType) {
                case DeviceConstants.MEDIA_TYPE_PHOTO:
                    mediaFile = new File(path + "IMG_" + timeStamp + ".jpg");
                    break;
                default:
                    return null;
            }
            Log.d(TAG, "File: " + Uri.fromFile(mediaFile));
            //5.Return the file URI
            return Uri.fromFile(mediaFile);
        } else {
            return null;
        }
    }
    private boolean isExternalStorageAvailable() {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

1 Answer

Floris De Feyter
Floris De Feyter
17,174 Points

I think your idea about "external storage" is not entirely right. "External" doesn't just mean "SD Card", from the android developers site: "Every Android-compatible device supports a shared "external storage" that you can use to save files. This can be a removable storage media (such as an SD card) or an internal (non-removable) storage."

The static variable Environment.DIRECTORY_PICTURES will return a directory to your "internal" external memory (more specifically, where all your pictures are stored). So if you want to save the file to an SD Card, you need to give the file path to your SD Card. This can be something like: "/mnt/external_sd/" or "/mnt/extSdCard/". Normally

Environment.getExternalStorageDirectory();

returns the correct path to the SD Card.

So then try (line 5):

File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "App Name");

Hi there many thanks for your response but data is still saving in the internal, ive check it and I dont know if there is an specific folder only in the sd card?