Find location of a removable SD card

前端 未结 22 2591
南方客
南方客 2020-11-21 11:32

Is there an universal way to find the location of an external SD card?

Please, do not be confused with External Storage.

Environment.getExternalStorage

22条回答
  •  感动是毒
    2020-11-21 11:45

    Since my original answer above, scanning vold is no longer viable across the various manufacturers.

    I've developed a more reliable and straight forward method.

    File mnt = new File("/storage");
    if (!mnt.exists())
        mnt = new File("/mnt");
    
    File[] roots = mnt.listFiles(new FileFilter() {
    
        @Override
        public boolean accept(File pathname) {
            return pathname.isDirectory() && pathname.exists()
                    && pathname.canWrite() && !pathname.isHidden()
                    && !isSymlink(pathname);
        }
    });
    

    roots will contain all the writeable root directories on the system, including any usb connected usb devices.

    NOTE: The canWrite method needs the android.permission.WRITE_EXTERNAL_STORAGE permission.

提交回复
热议问题