A Safe Path to External Storage

前端 未结 5 1619
暗喜
暗喜 2020-12-22 11:14

For my app, I have a fairly large database that needs to be stored on the user\'s device. I plan to change this in the future. For now, however, I\'d like to store it to the

5条回答
  •  隐瞒了意图╮
    2020-12-22 12:17

    Yep, KitKat now provides APIs for interacting with secondary external 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:

    
    

提交回复
热议问题