如何在Android中仅过滤掉相关的媒体文件? [英] How to filter out only relevant Media files in Android?

查看:186
本文介绍了如何在Android中仅过滤掉相关的媒体文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取手机中的所有音乐文件:

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

为此,我正在使用:

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();
}

但是上面的代码除了获取音乐文件之外,还正在获取一些其他不必要的文件,例如* sound_screen_on.mp3 *(已由其他一些应用安装并使用).

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).

问题是我的本地android音乐播放器未列出&播放这些不必要的文件.

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

如何过滤这些文件.

推荐答案

您可以使用

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);

  • 通过 Duration 进行过滤-音频文件的持续时间,以毫秒为单位
  • 通过 Type-Music 进行过滤-如果音频文件是音乐,则为非零
  • 通过 Type-RingTone 进行过滤-如果音频文件可能是铃声,则为非零值
  • 通过 Type-Alarm 进行过滤-如果音频文件可能是警报,则为非零值
    • 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
    • 查看更多参考

      这篇关于如何在Android中仅过滤掉相关的媒体文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆