Get .mp3 format files from android device

混江龙づ霸主 提交于 2019-12-21 05:29:09

问题


I'm developing a music player. I want to get all .mp3 format files from android device. But this code is not getting any files. My .mp3 files are in 'sdcard/music' folder. If i change the MEDIA_PATH = new String("sdcard/music"); like this it's getting files only from that music folder. But i need to get all .mp3 files from each and every folder in my external/internal memorycard. Please help me. this is my code.

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);
        }
    }

this is used to extract the .mp3 files

class FileExtensionFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".mp3") || name.endsWith(".MP3"));
    }

}

回答1:


  1. Query the media files
  2. Check if the file is an mp3 by checking its extension

The following method will return all the mp3 audio files in your device:

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;
}


来源:https://stackoverflow.com/questions/21496626/get-mp3-format-files-from-android-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!