I'm trying to access system folders on Android, /Music
to be precise. What I found in documentation is that getExternalFilesDir(Environment.DIRECTORY_MUSIC)
will grant access to music folder related to my app, which is part of it and eventually will be deleted once app gets removed from the device. So my question is how to access system music folder contents? Will I have the necessary permissions to grab these files and send over Bluetooth later on?
What I have done so far, yet it obviously lists app directory:
File path = getExternalFilesDir(Environment.DIRECTORY_MUSIC);
Log.d("Files", "Path: " + path);
File f = new File(path,"");
File file[] = f.listFiles();
for (int i=0; i < file.length; i++)
{
filesList.setText("FileName:" + file[i].getName());
}
To get a list of all the music on a device, you'll want to look into using Content Providers and MediaStore
Check out this code from Google's sample code for how to retrieve music from a device. It shows you exactly how to do it.
The important bit is this part...
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
// Perform a query on the content resolver. The URI we're passing specifies that we
// want to query for all audio media on external storage (e.g. SD card)
Cursor cur = mContentResolver.query(uri, null,
MediaStore.Audio.Media.IS_MUSIC + " = 1", null, null);
if (cur == null) {
// Query failed...
return;
}
if (!cur.moveToFirst()) {
// Nothing to query. There is no music on the device. How boring.
return;
}
// retrieve the indices of the columns where the ID, title, etc. of the song are
int artistColumn = cur.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int titleColumn = cur.getColumnIndex(MediaStore.Audio.Media.TITLE);
int albumColumn = cur.getColumnIndex(MediaStore.Audio.Media.ALBUM);
int durationColumn = cur.getColumnIndex(MediaStore.Audio.Media.DURATION);
int idColumn = cur.getColumnIndex(MediaStore.Audio.Media._ID);