'How to get file path inside folder structure
I am looking for a maybe better solution to get the path of a file inside a folder. For example:
I have the absolute path: "/storage/emulated/0/Mobile Pauker++/Lessons/Berge/Hoechste-Berge.pau.gz"
And I want to get: "Lessons/Berge/Hoechste-Berge.pau.gz"
My current solution is:
filepath.substring(filepath.indexOf("Mobile Pauker++")+15)
But I don't think that this is a good solution.
Solution 1:[1]
Please try this val path = File(Environment.getExternalStorageDirectory().toString() + File.separator + "Lessons/Berge/Hoechste-Berge.pau.gz")
Solution 2:[2]
I think I found a good solution:
filepath.split("${Environment.getExternalStorageDirectory()}${Constants.DEFAULT_APP_FILE_DIRECTORY}")[1]
I give the .split() method the path of my "root folder" and take the second part.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yash Shah |
| Solution 2 | user7174483 |
