如何使用MEDIASTORE获取所有音频文件

时间:2016-06-15 13:07:35

标签: android android-studio audio cursor mediastore

我正在尝试从移动设备获取所有音频文件,但我能够一次获得一个SD_card文件或内部存储文件,但我如何使用mediastore获取内部和外部文件信息以及我将进行哪些更改do .by将EXTERNAL_CONTENT_URI更改为INTERNAL_CONTENT_URI我能够获取内部文件,但是如何同时获取两者。我的音频转发方法

 private void parseAllAudio(String type) {
    try {
        String TAG = "Audio";
        Cursor cur = getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                null);

        if (cur == null) {
            // Query failed...
            Log.e(TAG, "Failed to retrieve music: cursor is null :-(");

        }
        else if (!cur.moveToFirst()) {
            // Nothing to query. There is no music on the device. How boring.
            Log.e(TAG, "Failed to move cursor to first row (no query results).");

        }else {
            Log.i(TAG, "Listing...");
            // retrieve the indices of the columns where the ID, title, etc. of the song are

            // add each song to mItems
            do {
                int artistColumn = cur.getColumnIndex(MediaStore.Audio.Media.ARTIST);
                int titleColumn = cur.getColumnIndex(MediaStore.Audio.Media.TITLE);
                int albumColumn = cur.getColumnIndex(MediaStore.Audio.Media.ALBUM);
                int durationColumn = cur.getColumnIndex(MediaStore.Audio.Media.DURATION);
                int idColumn = cur.getColumnIndex(MediaStore.Audio.Media._ID);
                int filePathIndex = cur.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                Log.i(TAG, "Title column index: " + String.valueOf(titleColumn));
                Log.i(TAG, "ID column index: " + String.valueOf(titleColumn));

                Log.i("Final ", "ID: " + cur.getString(idColumn) + " Title: " + cur.getString(titleColumn) + "Path: " + cur.getString(filePathIndex));
                MediaFileInfo audio = new MediaFileInfo();
                audio.setFileName(cur.getString(titleColumn));
                audio.setFilePath(cur.getString(filePathIndex));
                audio.setFileType(type);
                mediaList.add(audio);

            } while (cur.moveToNext());
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

0 个答案:

没有答案