用MediaStore类实现代码
时间: 2024-05-11 16:19:45 浏览: 176
MediaStore_Images_MediaTest.rar_Linux/Unix编程_Unix_Linux_
MediaStore类是Android提供的用于访问媒体文件的一个类,包括音频、视频、图片等。下面是一个使用MediaStore类获取音频文件信息的示例代码:
```
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.DURATION
};
Cursor cursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
null
);
if (cursor != null) {
while (cursor.moveToNext()) {
long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
long duration = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
// Do something with the retrieved data
}
cursor.close();
}
```
这段代码会查询设备上所有的音频文件,并且返回每个音频文件的ID、标题、艺术家、专辑和时长。你可以根据自己的需求修改projection数组中的内容,或者添加查询条件来过滤结果。
阅读全文