Download .mp3 files from an Android device.

I am developing a music player. I want to get all .mp3 files from an Android device. But this code does not receive any files. My .mp3 files are located in the 'sdcard / music' folder. If I change MEDIA_PATH = new String("sdcard/music");like this, it only gets files from this music folder. But I need to get all .mp3 files from each folder on my external / internal memory card. 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);
        }
    }

used to extract .mp3 files

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

}
+4
source share
1 answer
  • Media request
  • Check if the mp3 file is by checking its extension

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

Source: https://habr.com/ru/post/1524823/


All Articles