17

It is possible to get the Android device Internal Download Folder path?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Andrey
  • 231
  • 1
  • 2
  • 10
  • 1
    yes its possible.. [follow this](http://stackoverflow.com/questions/11351840/location-of-downloads-folder-for-devices-with-and-without-sd-card/11351896#11351896) – Dinithe Pieris Feb 12 '14 at 10:13

1 Answers1

37

If a device has an SD card, you use:

Environment.getExternalStorageState() 

If you don't have an SD card, you use:

Environment.getDataDirectory()

If there is no SD card, you can create your own directory on the device locally.

    //if there is no SD card, create new directory objects to make directory on device
        if (Environment.getExternalStorageState() == null) {
                        //create new file directory object
            directory = new File(Environment.getDataDirectory()
                    + "/RobotiumTestLog/");
            photoDirectory = new File(Environment.getDataDirectory()
                    + "/Robotium-Screenshots/");
            /*
             * this checks to see if there are any previous test photo files
             * if there are any photos, they are deleted for the sake of
             * memory
             */
            if (photoDirectory.exists()) {
                File[] dirFiles = photoDirectory.listFiles();
                if (dirFiles.length != 0) {
                    for (int ii = 0; ii <= dirFiles.length; ii++) {
                        dirFiles[ii].delete();
                    }
                }
            }
            // if no directory exists, create new directory
            if (!directory.exists()) {
                directory.mkdir();
            }

            // if phone DOES have sd card
        } else if (Environment.getExternalStorageState() != null) {
            // search for directory on SD card
            directory = new File(Environment.getExternalStorageDirectory()
                    + "/RobotiumTestLog/");
            photoDirectory = new File(
                    Environment.getExternalStorageDirectory()
                            + "/Robotium-Screenshots/");
            if (photoDirectory.exists()) {
                File[] dirFiles = photoDirectory.listFiles();
                if (dirFiles.length > 0) {
                    for (int ii = 0; ii < dirFiles.length; ii++) {
                        dirFiles[ii].delete();
                    }
                    dirFiles = null;
                }
            }
            // if no directory exists, create new directory to store test
            // results
            if (!directory.exists()) {
                directory.mkdir();
            }
        }// end of SD card checking

add permissions on your manifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Dinithe Pieris
  • 1,822
  • 3
  • 32
  • 44
  • 1
    I just created directory on my device without using SD card but directory is not showing on my device – saiRam89 Aug 04 '17 at 06:44
  • 1
    i also have this problem. –  Aug 08 '17 at 18:03
  • 9
    This answer is terribly confusing. Why is external storage the answer given when the question was for internal storage? Why is the answer a bunch of code, when the question is simply for the location of the directory? I found this answer looking for the location of the INTERNAL downloads folder. Shouldn't the answer look like /path/to/folder? – Altimus Prime Nov 06 '19 at 00:56