How to filter out only relevant Media files in Android?

我们两清 提交于 2019-12-21 07:55:35

问题


I'm trying to fetch all the music files in my phone:

For this I'm using:

String[] STAR = {"*"};

Uri allExternalSongUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";



Cursor cursor = getContentResolver().query(allExternalSongUri, STAR, selection, null, null);
if(cursor != null){
    if(cursor.moveToFirst()){
        do {
            String songName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
            Log.i("name", songName);
        } while (cursor.moveToNext());
    }
    cursor.close();
}

But above code, apart from getting music files, is also fetching some additional unnecessary files like *sound_screen_on.mp3* (which is installed & used by some other app).

Issue is my native android music player does not list & plays these unnecessary files.

How can I filter files like these.


回答1:


You can filter using MediaStore.Audio.AudioColumns class.That will return lots of value of Audio file and you can use it.

musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            proj, MediaStore.Audio.Media.DURATION + ">= 60000", null, null);
  • Filter by Duration - The duration of the audio file, in ms
  • Filter by Type-Music -Non-zero if the audio file is music
  • Filter by Type-RingTone - Non-zero if the audio file may be a ringtone
  • Filter by Type-Alarm - Non-zero if the audio file may be an alarm

Check for more reference




回答2:


Aside from the above methods suggested by others, in the case where column is not reliable from the db, you can always use MediaMetadataRetriever to get the information you want after the first filtering from the db.

http://developer.android.com/reference/android/media/MediaMetadataRetriever.html

This is of course slower than getting existing information from the media db, but it will give you author/album/duration etc other information and you can do as custom filter as you can get with the file list that you get from the ContentProvider.




回答3:


Using your same code, i dont get any undesired file.

Usually that files are marked to be ignored, so probably the media scanner did something wrong or the index is not updated in your system.

Sounds, images and other media files that are meant to be used by one app or by the system, are marked to be ignored simply by adding a file called ".nomedia" in the same directory. As you can read in the docs of the MediaStore provider

public static final String MEDIA_IGNORE_FILENAME

Added in API level 9 Name of the file signaling the media scanner to ignore media in the containing directory and its subdirectories. Developers should use this to avoid application graphics showing up in the Gallery and likewise prevent application sounds and music from showing up in the Music app.

Constant Value: ".nomedia"

So, besides of the advices from chintan khetiya, that are great, probably you can consider that there are some files marked to be ignored, that are not actually being ignored, and check it by yourself.

Simply, when you iterating the cursor, for every file, just check if in the same directory there is a file called like the value of MediaStore.MEDIA_IGNORE_FILENAME or the hardcoded .nomedia




回答4:


only get the music which the _data is more than a certain value, for example 300kb.



来源:https://stackoverflow.com/questions/21707954/how-to-filter-out-only-relevant-media-files-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!