From API level 19, Google has added an API .
Context.getExternalFilesDirs()Context.getExternalCacheDirs()Context.getObbDirs()
Applications are not allowed to write to external external storage devices, with the exception of their directories, defined in accordance with the allowed synthesized permissions. Limiting the recording in this way ensures that the system can clear files when applications are uninstalled.
Below is the approach to get the application directory on an external SD card with absolute paths.
Context _context = this.getApplicationContext(); File fileList2[] = _context.getExternalFilesDirs(Environment.DIRECTORY_DOWNLOADS); if(fileList2.length == 1) { Log.d(TAG, "external device is not mounted."); return; } else { Log.d(TAG, "external device is mounted."); File extFile = fileList2[1]; String absPath = extFile.getAbsolutePath(); Log.d(TAG, "external device download : "+absPath); appPath = absPath.split("Download")[0]; Log.d(TAG, "external device app path: "+appPath); File file = new File(appPath, "DemoFile.png"); try {
The output from the log from above is as follows:
context.getExternalFilesDirs() : /storage/extSdCard/Android/data/com.example.remote.services/files/Download external device is mounted. external device download : /storage/extSdCard/Android/data/com.example.remote.services/files/Download external device app path: /storage/extSdCard/Android/data/com.example.remote.services/files/
Sachin
source share