如何使用MediaStore只显示mp3文件?

时间:2013-02-18 11:33:28

标签: android

在我的应用中,我使用MediaStoreListAdaptor以及CursorLoader显示所有音频文件。但它显示所有音频文件(m4a,wav,ogg)。我只想显示mp3文件。我怎么能这样做?

String[] from = {MediaStore.MediaColumns.TITLE};
int[] to = {android.R.id.text1};

CursorLoader cursorLoader = new CursorLoader(this,sourceUri,null,null,null,MediaStore.Audio.Media.TITLE);
Cursor cursor = cursorLoader.loadInBackground();
startManagingCursor(cursor);
ListAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,cursor,from,to,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(adapter);

2 个答案:

答案 0 :(得分:8)

请尝试以下代码

ContentResolver cr = context.getContentResolver();

Uri uri = MediaStore.Files.getContentUri("external");

String[] projection = null;

String sortOrder = null;

String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";

String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("mp3"); 

String[] selectionArgsMp3 = new String[]{ mimeType };

Cursor allmp3Files = cr.query(uri, projection, selectionMimeType, selectionArgsmp3, sortOrder);

答案 1 :(得分:-1)

请参阅THIS示例,该示例仅显示提供的文件扩展名列表。