zoukankan      html  css  js  c++  java
  • 使用MeidaStore.Audio获得手机中的音频文件

    MediaStore是安卓系统自带的多媒体系统数据库,他在每次开机时刷新一次,可以通过Cursor这个类对数据库进行访问与修改,修改之后需用广播强制刷新。

    使用Cursor必须通过Context获得ContentResolver的对象实例。

    Cursor cursor1 = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,null);
    View Code

    代码中的context既是调用该函数的活动的对象,先上个封装了音乐信息的类

    public class Music {
        private String filename;
        private String title;
        private int during;
        private String singer;
        private String type;
        private String size;
        private String fileUrl;
        public Music(String filename,String title,int during,String singer,String type,String size,String fileUrl){
            this.filename = filename;
            this.title = title;
            this.during = during;
            this.singer = singer;
            this.type = type;
            this.size = size;
            this.fileUrl = fileUrl;
        }
        public String toString(){
            return "song[filename="+filename+", title="+title+", during="+during+", singer="+singer+", type="+type+", size="+size+", fileUrl="+fileUrl+"]";
        }
        public String getFilename() {
            return filename;
        }
        public void setFilename(String filename) {
            this.filename = filename;
        }
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public int getDuring() {
            return during;
        }
        public void setDuring(int during) {
            this.during = during;
        }
        public String getSinger() {
            return singer;
        }
        public void setSinger(String singer) {
            this.singer = singer;
        }
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
        public String getSize() {
            return size;
        }
        public void setSize(String size) {
            this.size = size;
        }
        public String getFileUrl() {
            return fileUrl;
        }
        public void setFileUrl(String fileUrl) {
            this.fileUrl = fileUrl;
        }
        
    }

    下面就是用Cursor操作数据库的帮助类了

    public class MusicHelper{
        public static List<Music> songs = new ArrayList<Music>();
        public static List<Music> getAllsongs(Context context){
                Cursor cursor1 = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,null);
                getcorrSongs(cursor1);
                Cursor cursor2 = context.getContentResolver().query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,null,null,null,null);
                getcorrSongs(cursor2);
            return songs;
        }
        public static void getcorrSongs(Cursor cursor){
            Music song = null;
            if(cursor != null){
                while(cursor.moveToNext()){
                    String filename = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
                    String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
                    int during = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
                    String singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                    String type = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE));
                    String size = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
                    String date = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                    song = new Music(filename,title,during,singer,type,size,date);
                    songs.add(song);
                }    
                cursor.close();
            }
        }
    }

    参数content即用活动的this引用传入。

    allsongs = MusicHelper.getAllsongs(this);
  • 相关阅读:
    初次学习Vue,输出Hello Vue!
    js的let语句在安卓手机端的QQ浏览器出错的问题
    前端框架的对比
    Vue环境搭建及node安装过程整理
    快速排序与冒泡排序(面试题)
    判断一个字符串中出现次数最多的字符并统计其出现的次数(面试题)
    Go_18: Golang 中三种读取文件发放性能对比
    GO_05_2:Golang 中 panic、recover、defer 的用法
    Go_17:GoLang中如何使用多参数属性传参
    Go_16:GoLang中flag标签使用
  • 原文地址:https://www.cnblogs.com/liboyan/p/4504239.html
Copyright © 2011-2022 走看看