这个测试ok
File externalFilesDir = getExternalFilesDir(null);
if (externalFilesDir != null) {
String appFolderPathOnSdCard = externalFilesDir.getAbsolutePath();
}
这个有时不管用:
public static File getSDRootPath(Context context) {
File sdPath;
boolean isSDExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); //SD卡是否存在
if (isSDExist) {
if (Build.VERSION.SDK_INT >= 28) {
File externalFileRootDir = context.getExternalFilesDir("");
do {
externalFileRootDir = Objects.requireNonNull(externalFileRootDir).getParentFile();
} while (Objects.requireNonNull(externalFileRootDir).getAbsolutePath().contains("/Android"));
sdPath = Objects.requireNonNull(externalFileRootDir);
} else {
sdPath = context.getExternalFilesDir("");
}
} else {
sdPath = Environment.getRootDirectory();//根目录
}
return sdPath;
}
File file=getSDRootPath(this);