Search code examples
androiddownloadstorageinternals

How can I get my Android device Internal Download Folder path?


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


Solution

  • 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" />