public static File addExternalDir(String path) {
    Path externalFileDir;
    if (path == null) {
      externalFileDir = null;
    } else {
      try {
        if (tmpExternalFilesDirBase == null) {
          tmpExternalFilesDirBase = RuntimeEnvironment.getTempDirectory().create("external-files-base");
        }
        externalFileDir = tmpExternalFilesDirBase.resolve(path);
        Files.createDirectories(externalFileDir);
        externalDirs.add(externalFileDir.toFile());
      } catch (IOException e) {
        throw new RuntimeException("Could not create external files dir", e);
      }
    }

    if (RuntimeEnvironment.getApiLevel() >= JELLY_BEAN_MR1
        && RuntimeEnvironment.getApiLevel() < KITKAT) {
      if (externalDirs.size() == 1 && externalFileDir != null) {
        Environment.UserEnvironment userEnvironment =
            ReflectionHelpers.getStaticField(Environment.class, "sCurrentUser");
        reflector(_UserEnvironment_.class, userEnvironment)
            .setExternalStorageAndroidData(externalFileDir.toFile());
      }
    } else if (RuntimeEnvironment.getApiLevel() >= KITKAT && RuntimeEnvironment.getApiLevel() < M) {
      Environment.UserEnvironment userEnvironment =
          ReflectionHelpers.getStaticField(Environment.class, "sCurrentUser");
      reflector(_UserEnvironment_.class, userEnvironment)
          .setExternalDirsForApp(externalDirs.toArray(new File[0]));
    }

    if (externalFileDir == null) {
      return null;
    }
    return externalFileDir.toFile();
  }
public static File addExternalDir(String path) {
    Path externalFileDir;
    if (path == null) {
      externalFileDir = null;
    } else {
      try {
        if (tmpExternalFilesDirBase == null) {
          tmpExternalFilesDirBase = RuntimeEnvironment.getTempDirectory().create("external-files-base");
        }
        externalFileDir = tmpExternalFilesDirBase.resolve(path);
        Files.createDirectories(externalFileDir);
        externalDirs.add(externalFileDir.toFile());
      } catch (IOException e) {
        throw new RuntimeException("Could not create external files dir", e);
      }
    }

    if (RuntimeEnvironment.getApiLevel() >= JELLY_BEAN_MR1
        && RuntimeEnvironment.getApiLevel() < KITKAT) {
      if (externalDirs.size() == 1 && externalFileDir != null) {
        Environment.UserEnvironment userEnvironment =
            ReflectionHelpers.getStaticField(Environment.class, "sCurrentUser");
        reflector(_UserEnvironment_.class, userEnvironment)
            .setExternalStorageAndroidData(externalFileDir.toFile());
      }
    } else if (RuntimeEnvironment.getApiLevel() >= KITKAT && RuntimeEnvironment.getApiLevel() < M) {
      Environment.UserEnvironment userEnvironment =
          ReflectionHelpers.getStaticField(Environment.class, "sCurrentUser");
      reflector(_UserEnvironment_.class, userEnvironment)
          .setExternalDirsForApp(externalDirs.toArray(new File[0]));
    }

    if (externalFileDir == null) {
      return null;
    }
    return externalFileDir.toFile();
  }
public static File addExternalDir ( String path ) { Path externalFileDir ; if ( path == null ) { externalFileDir = null ; } else { try { if ( tmpExternalFilesDirBase == null ) { tmpExternalFilesDirBase = RuntimeEnvironment . getTempDirectory ( ) . create ( "external-files-base" ) ; } externalFileDir = tmpExternalFilesDirBase . resolve ( path ) ; Files . createDirectories ( externalFileDir ) ; externalDirs . add ( externalFileDir . toFile ( ) ) ; } catch ( IOException e ) { throw new RuntimeException ( "Could not create external files dir" , e ) ; } } if ( RuntimeEnvironment . getApiLevel ( ) >= JELLY_BEAN_MR1 && RuntimeEnvironment . getApiLevel ( ) < KITKAT ) { if ( externalDirs . size ( ) == 1 && externalFileDir != null ) { Environment . UserEnvironment userEnvironment = ReflectionHelpers . getStaticField ( Environment . class , "sCurrentUser" ) ; reflector ( _UserEnvironment_ . class , userEnvironment ) . setExternalStorageAndroidData ( externalFileDir . toFile ( ) ) ; } } else if ( RuntimeEnvironment . getApiLevel ( ) >= KITKAT && RuntimeEnvironment . getApiLevel ( ) < M ) { Environment . UserEnvironment userEnvironment = ReflectionHelpers . getStaticField ( Environment . class , "sCurrentUser" ) ; reflector ( _UserEnvironment_ . class , userEnvironment ) . setExternalDirsForApp ( externalDirs . toArray ( new File [ 0 ] ) ) ; } if ( externalFileDir == null ) { return null ; } return externalFileDir . toFile ( ) ; }
Adds a directory to list returned by {@link ShadowUserEnvironment#getExternalDirs()}.

@param path the external dir to add
Adds a directory to list returned by { @link ShadowUserEnvironment#getExternalDirs () } .