I am developing a music player app that allows users to blacklist certain folders. The audio files from those folders will not be shown in the app.
I am using the MediaStore.Audio.AudioColumns.DATA
column to filter out audio files that is present inside those folders when querying MediaStore
for audio files.
I am using this logic to build a complete real path from the uri.
// Open folder picker
Intent intent = new Intent();
intent.setAction(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(intent, REQUEST_CODE_FOLDER_SELECT);
// Then in onActivityResult I am building the real path
Uri folder = data.getData();
String[] paths = Objects.requireNonNull(folder.getPath()).split(":");
// completePath looks like: /storage/emulated/0/Test/Another test
String completePath = Environment.getExternalStorageDirectory() +
(paths.length > 1 ? File.separator + paths[1] : "");
Currently, this works fine with folder that are present in primary external storage. But i would like users to select folders from external (removable) sdcards.
I am not sure how to build real folder path for folders present in (removable) sdcards.
I have come across a lot of solutions for "Get part from uri", but most of them were written before Android 10 came with Storage restrictions or only work for internal storage.
My real concern is that different vendors have different name and mounting paths for removable sdcards, hence i cannot hard code something like: "storage/sdcard/{relative folder path from uri}"