Use MediaStore
for the latest versions of Android, that's the recommended practice.
val projection = arrayOf(media-database-columns-to-retrieve)
val selection = sql-where-clause-with-placeholder-variables
val selectionArgs = values-of-placeholder-variables
val sortOrder = sql-order-by-clause
applicationContext.contentResolver.query(
MediaStore.media-type.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
sortOrder
)?.use { cursor ->
while (cursor.moveToNext()) {
// Use an ID column from the projection to get
// a URI representing the media item itself.
}
}
This is the basic structure of the API usage, you an read full details here.
Basically, you create a Directory in one of the default Android folders (in your case, it may be the 'Music' Directory), and then you read and write to that specific directory which is designated to your app.
Columns have default values, like name, thumbnail etc. You can get full URIs as well, there are a couple of methods available for that within the API, I think. You can read files using FileDescriptor
s like so
// Open a specific media item using ParcelFileDescriptor.
val resolver = applicationContext.contentResolver
// "rw" for read-and-write;
// "rwt" for truncating or overwriting existing file contents.
val readOnlyMode = "r"
resolver.openFileDescriptor(content-uri, readOnlyMode).use { pfd ->
// Perform operations on "pfd".
}
Maybe have a look here - How to get the Uri from MediaStore via file path?