Android 获取SD卡根目录方法

获取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;
}

 

posted @ 2022-06-20 21:30  swalka`x  阅读(1287)  评论(0编辑  收藏  举报