Find location of a removable SD card

前端 未结 22 2594
南方客
南方客 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:48

    I had an application that used a ListPreference where the user was required to select the location of where they wanted to save something.

    In that app, I scanned /proc/mounts and /system/etc/vold.fstab for sdcard mount points. I stored the mount points from each file into two separate ArrayLists.

    Then, I compared one list with the other and discarded items that were not in both lists. That gave me a list of root paths to each sdcard.

    From there, I tested the paths with File.exists(), File.isDirectory(), and File.canWrite(). If any of those tests were false, I discarded that path from the list.

    Whatever was left in the list, I converted to a String[] array so it could be used by the ListPreference values attribute.

    You can view the code here: http://sapienmobile.com/?p=204

提交回复
热议问题