Skip to content Skip to sidebar Skip to footer

Filter Mp3 Files From Other Media Files Using Mediastore & Mediametadataretriever?

I want to have a ListView on my app, which populates all mp3 files on my external storage. Here is the code in the onCreate method. @Override public void onCreate(Bundle savedInsta

Solution 1:

You can do it by below code which use MediaStore.Please add the external storage permission. android:name="android.permission.WRITE_EXTERNAL_STORAGE"

The code is -

publicArrayList<HashMap<String, String>> getSDCardAudioFiles() {
        // if you want  file path and additional detailsArrayList<HashMap<String, String>> audioFilesDetailList = newArrayList<HashMap<String, String>>();
        // if you want only file pathArrayList<String> audioFilePath =newArrayList<String>();
        // if you want only file nameArrayList<String> audioFileName =newArrayList<String>();
            Cursor mCursor = getContentResolver().query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    newString[] { MediaStore.Audio.Media.DISPLAY_NAME,
                            MediaStore.Audio.Media.DATA }, null, null, null);

            int count = mCursor.getCount();

            HashMap<String, String> audioFileMap;
            while (mCursor.moveToNext()) {
                audioFileMap = newHashMap<String, String>();
                audioFileMap.put("FileName",mCursor.getString(mCursor
                                .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)));
                audioFileMap.put("FilePath", mCursor.getString(mCursor
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
                if(audioFileMap.get("FileName").endsWith(".mp3")){
                    audioFilesDetailList.add(audioFileMap);

                    // if you want only file path
                    audioFilePath.add(audioFileMap.get("FilePath"));
                     // if you want only file name 
                    audioFileName.add(audioFileMap.get("FileName"));
                }
            }
            mCursor.close();
            //return the arraylist whichever u neededreturn audioFilesDetailList;
        }

Post a Comment for "Filter Mp3 Files From Other Media Files Using Mediastore & Mediametadataretriever?"