Neelay Srivastava Neelay Srivastava - 2 months ago 18
Android Question

How to get data from MediaStore.File and MediaStore.File.FileColumn

I am trying to get all the file name having audio files in it I have used Mediastore to get the mediastore audio,album,playlist and audio DATA also but how I can get the file or folder titles which contains the audio file .Here is the code that I have tried but it is not correct as I am not able to set the External_Content_uri.
This is the code I have tried.

private void External() {
try {
String[] proj = {MediaStore.Files.FileColumns.TITLE,
MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.PARENT,
MediaStore.Files.FileColumns.DATA
};// Can include more data for more details and check it.

String selection =MediaStore.Files.FileColumns.MEDIA_TYPE+"=?";

String[] selectionArgs = {"MediaStore.Files.FileColumns.MEDIA_TYPE_AUDIO"};

String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";

Cursor audioCursor = getContentResolver().query(MediaStore.Files.getContentUri("\"external\""), proj, selection, selectionArgs, sortOrder);

if (audioCursor != null) {
if (audioCursor.moveToFirst()) {
do {
int filetitle = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE);
int file_id = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID);
int fileparent = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.PARENT);
int filedata = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
Mediafileinfo info = new Mediafileinfo();
info.setData(new File(new File(audioCursor.getString(filedata)).getParent()).getName());
info.setTitle(audioCursor.getString(filetitle));
info.set_id(audioCursor.getString(file_id));
info.setParent(audioCursor.getString(fileparent));
// info.setData(audioCursor.getString(filedata));
audioList.add(info);
} while (audioCursor.moveToNext());
}
}
assert audioCursor != null;
audioCursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}


I tried this and this example but I am not able to get the solution.

Answer

Writing a fresh answer, since all you want is the folder names of the audio files.

So the best thing to use here is MediaStore.Audio.Media instead of using MediaStore.File. Get the folder names using the below on the audio file path.

new File(new File(audioCursor.getString(filedata)).getParent()).getName()

private void External() {
try {
    Uri externalUri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

    projection=new String[]{MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.DATA,;

    String selection =null;

    String[] selectionArgs = null;

    String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";

    Cursor audioCursor = getContentResolver().query(externalUri, proj, selection, selectionArgs, sortOrder);

    if (audioCursor != null) {
        if (audioCursor.moveToFirst()) {
            do {
                int filetitle = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
                int file_id = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
                int filePath = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
                Mediafileinfo info = new Mediafileinfo();
                info.setData(new File(new File(audioCursor.getString(filedata)).getParent()).getName());
                info.setTitle(audioCursor.getString(filetitle));
                info.set_id(audioCursor.getString(file_id));
                audioList.add(info);
            } while (audioCursor.moveToNext());
        }
    }
    assert audioCursor != null;
    audioCursor.close();
} catch (Exception e) {
    e.printStackTrace();
}
}
Comments