获取SD卡根目录,兼容Android10及以上版本的方法
public File getRootFile(Context context) { File file; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // /storage/emulated/0/Android/data/com.example.demo/files File externalFileDir = context.getExternalFilesDir(null); do { externalFileDir = Objects.requireNonNull(externalFileDir).getParentFile(); } while(Objects.requireNonNull(externalFileDir).getAbsolutePath().contains("/Android")); file = Objects.requireNonNull(externalFileDir); } else { file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile().toURI()); } return file; }
原创文章,作者:端木书台,如若转载,请注明出处:https://blog.ytso.com/273251.html