How to get data from MediaStore.File and MediaStore.File.FileColumn
Solution 1
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();
}
}
Solution 2
Hi query media file like audio,images and video using Android Media Store and Android Content Resolver, check this tutorial
http://www.androiddevelopersolutions.com/2015/12/android-media-store-tutorial-list-all.html
For Listing All images:
private void parseAllImages() {
try {
String[] projection = {MediaStore.Images.Media.DATA};
cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
int size = cursor.getCount();
/******* If size is 0, there are no images on the SD Card. *****/
if (size == 0) {
} else {
int thumbID = 0;
while (cursor.moveToNext()) {
int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
/**************** Captured image details ************/
/***** Used to show image on view in LoadImagesFromSDCard class ******/
String path = cursor.getString(file_ColumnIndex);
String fileName = path.substring(path.lastIndexOf("/") + 1, path.length());
MediaFileInfo mediaFileInfo = new MediaFileInfo();
mediaFileInfo.setFilePath(path);
mediaFileInfo.setFileName(fileName);
mediaFileInfo.setFileType(type);
mediaList.add(mediaFileInfo);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
For Listing all Video(.mp4)
private void parseAllVideo() {
try {
String name = null;
String[] thumbColumns = {MediaStore.Video.Thumbnails.DATA,
MediaStore.Video.Thumbnails.VIDEO_ID};
int video_column_index;
String[] proj = {MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE};
Cursor videocursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
int count = videocursor.getCount();
Log.d("No of video", "" + count);
for (int i = 0; i < count; i++) {
MediaFileInfo mediaFileInfo = new MediaFileInfo();
video_column_index = videocursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
videocursor.moveToPosition(i);
name = videocursor.getString(video_column_index);
mediaFileInfo.setFileName(name);
int column_index = videocursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
videocursor.moveToPosition(i);
String filepath = videocursor.getString(column_index);
mediaFileInfo.setFilePath(filepath);
mediaFileInfo.setFileType(type);
mediaList.add(mediaFileInfo);
// id += " Size(KB):" +
// videocursor.getString(video_column_index);
}
videocursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}
For Listing All Audio
private void parseAllAudio() {
try {
String TAG = "Audio";
Cursor cur = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
null);
if (cur == null) {
// Query failed...
Log.e(TAG, "Failed to retrieve music: cursor is null :-(");
}
else if (!cur.moveToFirst()) {
// Nothing to query. There is no music on the device. How boring.
Log.e(TAG, "Failed to move cursor to first row (no query results).");
}else {
Log.i(TAG, "Listing...");
// retrieve the indices of the columns where the ID, title, etc. of the song are
// add each song to mItems
do {
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);
int filePathIndex = cur.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
Log.i(TAG, "Title column index: " + String.valueOf(titleColumn));
Log.i(TAG, "ID column index: " + String.valueOf(titleColumn));
Log.i("Final ", "ID: " + cur.getString(idColumn) + " Title: " + cur.getString(titleColumn) + "Path: " + cur.getString(filePathIndex));
MediaFileInfo audio = new MediaFileInfo();
audio.setFileName(cur.getString(titleColumn));
audio.setFilePath(cur.getString(filePathIndex));
audio.setFileType(type);
mediaList.add(audio);
} while (cur.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
}
}
Neelay Srivastava
I am doing my B.tech currently And I love doing android.I made this https://play.google.com/store/apps/details?id=com.unnionapp.mediaplayer
Updated on June 25, 2022Comments
-
Neelay Srivastava almost 2 years
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.
-
Neelay Srivastava over 7 yearsI am able to able to get all the data thx to your post i think app to get all types of data but here I want to Use Mediastore.file
-
Neelay Srivastava over 7 yearsI am able to get all the things you put in your answer I want to get the Files with media on it not the Audio.Data
-
Neelay Srivastava over 7 yearscan you tell me how I can use the Mediastore.Files or where i am doing wrong
-
Neelay Srivastava over 7 yearsBro i am able to get every thing in MediaStore.Audio section but i want MediaStore.Files
-
Darshan Miskin over 7 yearsyou want file names of all music files, got that. Did you try the column value replacement with column name as mentioned in the answer?
-
Neelay Srivastava over 7 yearsI want to get the folder name that contains the media files
-
Neelay Srivastava over 7 yearsI tried ever possible way but I cant find any thing
-
Darshan Miskin over 7 yearsnames of folder which contain any type of media?
-
Neelay Srivastava over 7 yearsNo thats why MEDIA_TYPE_AUDIO so i can get the audio files
-
Neelay Srivastava over 7 yearsEver used Media store.Files ??
-
Neelay Srivastava over 7 yearsLet us continue this discussion in chat.
-
charitha amarasinghe almost 4 yearscan't we use MediaStore , this method for documents?