How can I get the external SD card path for Android 4.0+?

前端 未结 26 2686
清歌不尽
清歌不尽 2020-11-22 04:48

Samsung Galaxy S3 has an external SD card slot, which is mounted to /mnt/extSdCard.

How can I get this path by something like Environment.getExter

26条回答
  •  清酒与你
    2020-11-22 05:48

    Thanks for the clues provided by you guys, especially @SmartLemon, I got the solution. In case someone else need it, I put my final solution here( to find the first listed external SD card ):

    public File getExternalSDCardDirectory()
    {
        File innerDir = Environment.getExternalStorageDirectory();
        File rootDir = innerDir.getParentFile();
        File firstExtSdCard = innerDir ;
        File[] files = rootDir.listFiles();
        for (File file : files) {
            if (file.compareTo(innerDir) != 0) {
                firstExtSdCard = file;
                break;
            }
        }
        //Log.i("2", firstExtSdCard.getAbsolutePath().toString());
        return firstExtSdCard;
    }
    

    If no external SD card there, then it returns the on board storage. I will use it if the sdcard is not exist, you may need to change it.

提交回复
热议问题