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

前端 未结 26 3475
清歌不尽
清歌不尽 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:35

    Good news! In KitKat there's now a public API for interacting with these secondary shared storage devices.

    The new Context.getExternalFilesDirs() and Context.getExternalCacheDirs() methods can return multiple paths, including both primary and secondary devices. You can then iterate over them and check Environment.getStorageState() and File.getFreeSpace() to determine the best place to store your files. These methods are also available on ContextCompat in the support-v4 library.

    Also note that if you're only interested in using the directories returned by Context, you no longer need the READ_ or WRITE_EXTERNAL_STORAGE permissions. Going forward, you'll always have read/write access to these directories with no additional permissions required.

    Apps can also continue working on older devices by end-of-lifing their permission request like this:

    
    

提交回复
热议问题