0

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.

user7174483
  • 73
  • 1
  • 9

2 Answers2

0

Please try this val path = File(Environment.getExternalStorageDirectory().toString() + File.separator + "Lessons/Berge/Hoechste-Berge.pau.gz")

Yash Shah
  • 52
  • 4
  • Isn't that the opposite way to what I want? I want to separate the path after the "Mobile Pauker++". – user7174483 May 17 '22 at 12:52
  • @user7174483: Please check this [link](https://stackoverflow.com/questions/26570084/how-to-get-file-name-from-file-path-in-android) i think it's might be what you want. – Yash Shah May 17 '22 at 12:58
  • This question is only about getting the name, but I need an individual part of a path. I postet a possible solution which is good in my opinion. – user7174483 May 17 '22 at 13:21
0

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.

user7174483
  • 73
  • 1
  • 9