如何在Android上使用ContentResolver.Query从SdCard获取mp3文件列表?

时间:2014-02-07 16:18:43

标签: android android-contentresolver android-cursor

我在mnt / sdcard中创建了一个Cuzler文件夹并放了一些mp3文件..这里编码我正在使用的..

File cuzFolder = new File(Environment.getExternalStorageDirectory() + "/Cuzler/");
String storageFolder = cuzFolder.getPath(); 

Uri urlCuz = Uri.parse(storageFolder); 
Log.i(TAG, "CUZ: " + urlCuz.toString());
Cursor cur = mContentResolver.query(urlCuz, null,

MediaStore.Audio.Media.IS_MUSIC + " = 1", null, null);
Log.i(TAG, "Query finished. " + (cur == null ? "Returned NULL." : "Returned a cursor."));

cur返回null ..我已经给出了写/读权限。我正在制作一个MP3播放器,它将在sdcard的文件夹中播放一些mp3,路径是真的,因为我正在将mp3下载到这个文件夹,没问题。但我不明白为什么查询返回null?

我也试过像

这样的东西
Uri urlCuz = Uri.parse("content://" + storageFolder); 

并提供直接链接,如mnt / sdcard / Cuzler或/ mnt / scard / Cuzler

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

public class MainActivity extends AppCompatActivity {

    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.list_view);

        ContentResolver resolver = MainActivity.this.getContentResolver();
        ArrayList<String> stringArrayList = new ArrayList<String>();


        String GENRE_ID      = MediaStore.Audio.Genres._ID;
        String GENRE_NAME    = MediaStore.Audio.Genres.NAME;
        String SONG_ID       = android.provider.MediaStore.Audio.Media._ID;
        String SONG_TITLE    = android.provider.MediaStore.Audio.Media.TITLE;
        String SONG_ARTIST   = android.provider.MediaStore.Audio.Media.ARTIST;
        String SONG_ALBUM    = android.provider.MediaStore.Audio.Media.ALBUM;
        String SONG_YEAR     = android.provider.MediaStore.Audio.Media.YEAR;
        String SONG_TRACK_NO = android.provider.MediaStore.Audio.Media.TRACK;
        String SONG_FILEPATH = android.provider.MediaStore.Audio.Media.DATA;
        String SONG_DURATION = android.provider.MediaStore.Audio.Media.DURATION;


        String[] genreColumns = {
               SONG_ID,
                SONG_TITLE
        };

        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (shouldShowRequestPermissionRationale(
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
                // Explain to the user why we need to read the contacts
            }

            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    1);

            // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
            // app-defined int constant that should be quite unique

            return;
        }

        Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,genreColumns,null,null,null);
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
//            genreIdToGenreNameMap.put(cursor.getString(0), cursor.getString(1));
             stringArrayList.add(cursor.getString(1));

        cursor.close();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,stringArrayList);
        listView.setAdapter(adapter);

    }
}