Android多媒体数据库及MediaProvider

image, video, audio的contentUri是一样的.他们的getContentUri()方法都是如下:比如audio的实在MediaStore.Audio.Media.getContentUri(),层层的内部类结构.

 

public static Uri getContentUri(String volumeName) {
                return Uri.parse(CONTENT_AUTHORITY_SLASH + volumeName +
                        "/audio/media");
            }


Audio中还有Albums, Artists, Playlists,AlbumColumns, ArtistColumns,Media ,AudioColumns.

 

如果要某个艺术家有多少歌或者这个艺术家有多少个专辑在数据库中,就要使用MediaStore.Audio.Artists.getContentUri(),然后返回MediaStore.Audio.ArtistColumns.NUMBER_OF_ALBUMS或者MediaStore.Audio.ArtistColumns.NUMBER_OF_TRACKS这两列.如果要查询某个专辑有多少歌

排序:

使用各种key字段,比如拿MediaStore.Audio.AudioColumns.TITLE_KEY.,也可以用MediaStore.Audio.AritistColumns.ARTIST_KEY...每个key都是一个字母.

专辑图片,歌手图片:

MediaStore.Audio.AlbumColumns.ALBUM_ARTIST,使用这个列名可以获取.

每一首歌都对应的专辑id和艺术家id,

 

监听多媒体数据库

继承ContentObserver,复写其onChange()方法,然后使用getContentResolve().register(Uri)注册.

MusicCenterActivity#scanNewFiles( )处理新增的音乐文件:

 

private void scanNewFiles(){
        try {
            //获取原来最大时间,也就是最后一首歌曲存储的时间
            long maxInPref = MusicPreference.getMaxDateAdded(getApplicationContext());
            LogUtil.i(TAG, "maxInPref: "+maxInPref);
            if(maxInPref != -1){
                //获取文件Meida数据库中新文件
                ArrayList newFiles = MediaDBUtils.getFileListNewAdded(getApplicationContext(), maxInPref);
                for(int i=0;i

获取当前文件中最大添加日期,在下次扫描时,扫描条件是添加日期大于该日期的就是新增的文件.

 

 

public static long getMaxDateAdded(Context context){
    	long res = -1;
        Cursor c = null;
        try {
        	String[] cols = new String[] {"_id", "max(date_added) as _max"};
        	c = CursorUtils.query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
                    cols, MediaStore.Audio.Media.IS_MUSIC +" = \"1\" ", null, null);
            if(c != null){
            	c.moveToFirst();
                res = c.getLong(c  
                        .getColumnIndexOrThrow("_max"));
            }
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(c != null)
                c.close();
        }
    	return res;
    }


MusicCenterActivity#scanDeletedFiles()处理已删除文件:

 

将Music数据库和多媒体数据库的逐条对比,保存已删除路径作为删除的whereArgs

 

 private String getAudioPathsToDelete(Cursor cursor){
    	if(null == cursor || cursor.getCount() == 0)
    		return null;
    	StringBuilder sb = new StringBuilder();
    	sb.append('(');
    	cursor.moveToFirst();
    	while(!cursor.isAfterLast()){
    		String dataPath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA));
    		Cursor result = CursorUtils.query(MusicCenterActivity.this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,
    				MediaStore.Audio.AudioColumns.DATA + "=?", new String[]{dataPath}, null);
    		if(null == result || result.getCount() == 0){
    			sb.append('\'');
    			sb.append(dataPath);
    			sb.append("\',");
    		}
    		cursor.moveToNext();
    	}
    	if(sb.length() == 1){
    		return null;
    	}
    	try{
    		sb.replace(sb.length()-1, sb.length(), ")");
    	LogUtil.i(TAG, "sb:   " + sb.toString());
    	}catch(StringIndexOutOfBoundsException e){
    		e.printStackTrace();
    	}
    	
    	return sb.toString();
    }
    
    private int deleteFromMediaProvider(String audioPaths){
    	if(null == audioPaths)
    		return 0;
    	StringBuilder where  = new StringBuilder(MediaStore.Audio.AudioColumns.DATA);
    	where.append(" IN ");
    	where.append(audioPaths);
    	LogUtil.i(TAG, where.toString());
    	int rowsDeletedNum = getContentResolver().delete(MusicDBUtils.URI_AUDIO, where.toString(), null);
    	return rowsDeletedNum;
    }
    
    private void scanFilesDeleted(){
				// TODO Auto-generated method stub
				ContentResolver contentResolver = getContentResolver();
    	        Cursor c = contentResolver.query(MusicDBUtils.URI_AUDIO, new String[]{MediaStore.Audio.AudioColumns.DATA}, null, null, null);
    	        String paths = getAudioPathsToDelete(c);
    	        int count = 0;
    	        count = deleteFromMediaProvider(paths);
    	        LogUtil.i(TAG, "audio deleted num:  " + count);
    }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(移动开发综合,模块相关)