我正在开发一个音乐播放器。我想从Android设备获取所有.mp3格式的文件。
但是这段代码没有得到任何文件。我的.mp3文件位于'sdcard / music'文件夹中。如果我像这样更改MEDIA_PATH = new String("sdcard/music");
,它只能从该音乐文件夹中获取文件。但我需要从外部/内部存储卡中的每个文件夹中获取所有.mp3文件。请帮我。
这是我的代码。
final String MEDIA_PATH = new String(Environment.getExternalStorageDirectory());
public void Generate_Database(){
File home = new File(MEDIA_PATH);
if (home.listFiles(new FileExtensionFilter()).length > 0) {
for (File file : home.listFiles(new FileExtensionFilter())) {
String title = file.getName().substring(0, (file.getName().length() - 4));
String path = file.getPath();
mediaInfo.setDataSource(path);
String albumName = "unknown",artist = "unknown",genere = "unknown",duration = "unknown",composer = "unknown";
if(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM) == null)
albumName = "unknown";
else{
albumName = mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
}
if(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST) == null)
artist = "unknown";
else{
artist = mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
}
if(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE) == null)
genere = "unknown";
else{
genere = mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);
}
if(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION) == null)
duration = "unknown";
else{
duration = mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
}
if(mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPOSER) == null)
composer = "unknown";
else{
composer = mediaInfo.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPOSER);
}
//Toast.makeText(getApplicationContext(), title+path+ albumName+artist+ genere+ duration+ composer, Toast.LENGTH_LONG).show();
ds.createEntry2(title, path, albumName, artist, genere, duration, composer);
}
}
这用于提取.mp3文件
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".mp3") || name.endsWith(".MP3"));
}
}
答案 0 :(得分:14)
以下方法将返回设备中的所有mp3音频文件:
private List<String> scanDeviceForMp3Files(){
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projection = {
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION
};
final String sortOrder = MediaStore.Audio.AudioColumns.TITLE + " COLLATE LOCALIZED ASC";
List<String> mp3Files = new ArrayList<>();
Cursor cursor = null;
try {
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
cursor = getContentResolver().query(uri, projection, selection, null, sortOrder);
if( cursor != null){
cursor.moveToFirst();
while( !cursor.isAfterLast() ){
String title = cursor.getString(0);
String artist = cursor.getString(1);
String path = cursor.getString(2);
String displayName = cursor.getString(3);
String songDuration = cursor.getString(4);
cursor.moveToNext();
if(path != null && path.endsWith(".mp3")) {
mp3Files.add(path);
}
}
}
// print to see list of mp3 files
for( String file : mp3Files) {
Log.i("TAG", file);
}
} catch (Exception e) {
Log.e("TAG", e.toString());
}finally{
if( cursor != null){
cursor.close();
}
}
return mp3Files;
}