列出存储设备内特定文件夹的mp3文件

时间:2018-01-15 07:59:48

标签: android

我一直在构建简单的mp3播放器一切顺利,从存储设备中取出所有mp3文件的代码运行良好。

Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

我的问题我只需要从sdcard kalid文件夹加载mp3文件。我试过这个代码,但它没有从kalid文件夹中加载mp3文件。

File path = new File(Environment.getExternalStorageDirectory() + "/kalid");
Uri media = Uri.fromFile(path);

我也厌倦了这段代码

File path = new File(Environment.getExternalStorageDirectory() + "/kalid");
Uri media = MediaStore.Audio.Media.getContentUriForPath(String.valueOf(path));

我认为没有人理解我的问题,这里是完整的源代码  ContentResolver resolver = context.getContentResolver();

Uri media = MediaStore.Audio.Media.getContentUriForPath("Environment.getExternalStorageDirectory().getPath()+ kalid+");


    String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
    Cursor cursor = resolver.query(media, PROJECTION, selection, null, null);
    if (cursor == null || cursor.getCount() == 0) {
        Log.d(TAG, "Music List Empty");
        return false;
    }

    mMusicList.clear(); // clear

    int count = cursor.getCount();

    for (int i = 0; i != count; ++i) {
        if (!cursor.moveToNext()) {
            break;
        }
        //Log.d(TAG, cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
        // Add to ArrayList
        mMusicList.add(new Music(
                cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)),
                cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)),
                cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)),
                cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)),
                cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST))
                ));

    }
    cursor.close();

1 个答案:

答案 0 :(得分:0)

最后,在尝试了很多东西后,我在kalid文件夹中列出了mp3文件,这里是源代码。

 Cursor cursor = resolver.query(media, 
PROJECTION, 
MediaStore.Audio.Media.DATA + " like ? ",
new String[]{"%kalid%"}, null);