• 获取sd的音乐文件



     

    获取sd卡的音乐文件的方法

    public class Music {
        public static ArrayList<MusicMessage> Music(Context context) {
            ContentResolver resolver = context.getContentResolver();
            // 内容提供者访问MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
            Cursor cursor = resolver.query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                    null);
            ArrayList<MusicMessage> list = new ArrayList<MusicMessage>();
            while (cursor.moveToNext()) {
                // 名字
                String name = cursor.getString(cursor
                        .getColumnIndex(Audio.Media.TITLE));
                // 专辑名
                String album = cursor.getString(cursor
                        .getColumnIndexOrThrow(Audio.Media.ALBUM));
                // 歌手名
                String artist = cursor.getString(cursor
                        .getColumnIndexOrThrow(Audio.Media.ARTIST));
                // URI歌曲文件存放路径
                String path = cursor.getString(cursor
                        .getColumnIndexOrThrow(Audio.Media.DATA));
                // 歌曲文件播放时间长度
                int duration = cursor.getInt(cursor
                        .getColumnIndexOrThrow(Audio.Media.DURATION));
                // 音乐文件大小
                int size = cursor.getInt(cursor
                        .getColumnIndexOrThrow(Audio.Media.SIZE));
                list.add(new MusicMessage(false, name, album, artist, path,
                        duration, size));
            }
            return list;
        }
    }

    vo类

    public class MusicMessage {
    
        public MusicMessage(boolean b, String name, String album, String artist,
                String path, int duration, int size) {
            super();
            this.b = b;
            this.name = name;
            this.album = album;
            this.artist = artist;
            this.path = path;
            this.duration = duration;
            this.size = size;
        }
        @Override
        public String toString() {
            return "MusicMessage [b=" + b + ", name=" + name + ", album=" + album
                    + ", artist=" + artist + ", path=" + path + ", duration="
                    + duration + ", size=" + size + "]";
        }
        public boolean isB() {
            return b;
        }
        public void setB(boolean b) {
            this.b = b;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getAlbum() {
            return album;
        }
        public void setAlbum(String album) {
            this.album = album;
        }
        public String getArtist() {
            return artist;
        }
        public void setArtist(String artist) {
            this.artist = artist;
        }
        public String getPath() {
            return path;
        }
        public void setPath(String path) {
            this.path = path;
        }
        public int getDuration() {
            return duration;
        }
        public void setDuration(int duration) {
            this.duration = duration;
        }
        public int getSize() {
            return size;
        }
        public void setSize(int size) {
            this.size = size;
        }
        public boolean b;
        public String name;
        public String album;
        public String artist;
        public String path;
        public int duration;
        public int size;
    
    }

    调用方法

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            List<MusicMessage> list = Music.Music(MainActivity.this);
            System.out.println(list.size());
            System.out.println(list.toString());
            ListView listview = (ListView) findViewById(R.id.listview);
            listview.setAdapter(new Myadapter(list, MainActivity.this));
    
        }
    
    }
  • 相关阅读:
    java多线程(八)-死锁问题和java多线程总结
    java多线程(七)-线程之间的 协作
    java多线程(六)-线程的状态和常用的方法
    在SOUI中使用网格布局
    SOUI视频教程
    在SOUI中使用动态多语言切换
    在SOUI中使用窗口自適應大小
    在SOUI中使用线性布局
    搜索引擎广告过滤Chrome插件
    第三十四篇:在SOUI中使用异步通知
  • 原文地址:https://www.cnblogs.com/3674-it/p/5408355.html
Copyright © 2020-2023  润新知