[英]How to get all music files stored in internal storage and external storage in android using MediaStore?
I am trying to list all songs in the internal storage and external storage for my application as a list view. 我正在尝试将我的应用程序的内部存储和外部存储中的所有歌曲作为列表视图列出。 I am using the below code for fetching songs
我正在使用以下代码来获取歌曲
ContentResolver musicResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
if(musicCursor!=null && musicCursor.moveToFirst()){
//get columns
int titleColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.ARTIST);
int durationColumn=musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DURATION);
do {
try {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
String duration = musicCursor.getString(durationColumn);
if (!thisArtist.equalsIgnoreCase("<unknown>")) {
//save track
}
} catch (Exception e) {
}
}
while (musicCursor.moveToNext());
The above code is successfully fetching all the songs in the external storage but is not fetching the songs stored in the internal memory. 上面的代码成功获取了外部存储器中的所有歌曲,但没有获取内部存储器中的歌曲。 This is a problem in devices like Samsung Galaxy S8 where there is no external storage.
在没有外部存储的设备如Samsung Galaxy S8中,这是一个问题。 I have also tried the cursor with the below uri
我也尝试了以下uri的光标
musicUri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
This fetches me the device ringtones and SMS tones which are not what I need. 这使我获得了我不需要的设备铃声和SMS铃声。
I have gone through similar questions in StackOverflow and the answers there does not solve this problem for me. 我在StackOverflow中经历了类似的问题,那里的答案并没有为我解决这个问题。
Please help. 请帮忙。 Thanks.
谢谢。
I think the word EXTERNAL is confusing. 我认为EXTERNAL一词令人困惑。 It is used to address internal memory The scanning process will enter all your music into the media database irrespective of whether it is on internal or external media.(just make sure there is no hidden .nomedia file in the folder).
它用于寻址内部存储器扫描过程将把所有音乐输入到媒体数据库,而不管它是在内部还是外部媒体上。(请确保文件夹中没有隐藏的.nomedia文件)。 The code below should return all your music.
以下代码应返回您的所有音乐。 If it does not then I suspect your media database is not fully populated.
如果不是,那么我怀疑您的媒体数据库未完全填充。
public Cursor getAllTracks(Context context) {
// gets all tracks
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
ContentResolver cr = context.getContentResolver();
final String[] columns = {track_id ,track_no, artist, track_name,
album, duration, path, year, composer};
return cr.query(uri, columns, null, null, null);
}
this works for me. 这对我有用。 try this
尝试这个
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
SongInfo s = new SongInfo(name, artist, url);
_songs.add(s);
} while (cursor.moveToNext());
}
cursor.close(); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.