Building a Utility to get the path to External Removable storage every time

前端 未结 1 1563
挽巷
挽巷 2020-12-18 08:14

Recently, I needed to be able to write some data strictly on a phone\'s external, removable storage (aka SDCard). For lower-end devices without \"external\" internal storage

相关标签:
1条回答
  • 2020-12-18 09:09

    I'm not sure how reliable this would be but since the removable SD card contains the LOST.DIR folder at its root and gets recreated upon boot (in cases where the user might delete it)...

        File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
        for (File sub : root.listFiles()) {
            if (new File(sub + "/lost.dir").exists()) {
                // 'sub' is probably the removable SD
                break;
            }
        }    
    

    If 'sub' is found with a LOST.DIR folder , might want to write its value to sharedPrefs so that this lookup is only performed one time (if there is no value stored) and then just fetched from prefs, OR so that the app knows this path even in case the user deleted LOST.DIR and it currently doesn't exist anywhere.

    0 讨论(0)
提交回复
热议问题