android关闭cursor的方法

今天review了一下之前写播发器的代码,发现了个问题,在音乐数据信息查询后,没有及时关闭cursor,虽然目前使用没什么大问题,可是在音乐数据比较多时,可能会有不好的影响,所以我还是觉得有必要对Cursor添加关闭处理,再贴一下音乐播放器《Android音乐播放器 -- 数据处理》中数据处理的代码

/** 
 * 用于从数据库中查询相同专辑的歌曲信息,保存在List集合当中 
 *  
 * @return 
 */  
public static List<AlbumInfo> getAlbumCntInfos(Context context) {  
    Cursor cursor = context.getContentResolver().query(  
            MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,   
            null, null, null, null);  
      
    List<AlbumInfo> albumInfos = new ArrayList<AlbumInfo>();  
    for (int i = 0; i < cursor.getCount(); i++) {  
        cursor.moveToNext();  
        AlbumInfo albumInfo = new AlbumInfo();  
        String album = cursor.getString(cursor  
                .getColumnIndex(MediaStore.Audio.Albums.ALBUM));    //专辑  
        int albumCount = cursor.getInt(cursor  
                .getColumnIndex(MediaStore.Audio.Albums.NUMBER_OF_SONGS)); // 该专辑一共有多少歌曲  
  
        albumInfo.setAlbum(album);  
        albumInfo.setAlbumCnt(albumCount);  
        albumInfos.add(albumInfo);  
  
    }  
    return albumInfos;  
}  
我们可以看到,我们使用cursor查询数据,并将相关数据存储到albumInfos这个集合中,查询完毕即可直接关闭,那我们可以直接在return前加cursor.close就可以了?答案是否定的,因为如果前面的查询代码有exception,那也不会运行到cursor.close,针对这种情况,我们可以考虑用try..catch..finally的方式进行关闭cursor.这样,如果既是有exception发生,也同样会关闭cursor,下面就是我们修改之后的代码:

public static List<AlbumInfo> getAlbumCntInfos(Context context) {  
	Cursor cursor = null;
	AlbumInfo albumInfo = new AlbumInfo();  
	
    try{
		cursor = context.getContentResolver().query(  
            MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,   
            null, null, null, null);     
		List<AlbumInfo> albumInfos = new ArrayList<AlbumInfo>();  
		for (int i = 0; i < cursor.getCount(); i++) {  
        cursor.moveToNext();  
        
        String album = cursor.getString(cursor  
                .getColumnIndex(MediaStore.Audio.Albums.ALBUM));    //专辑  
        int albumCount = cursor.getInt(cursor  
                .getColumnIndex(MediaStore.Audio.Albums.NUMBER_OF_SONGS)); // 该专辑一共有多少歌曲  
  
        albumInfo.setAlbum(album);  
        albumInfo.setAlbumCnt(albumCount);  
        albumInfos.add(albumInfo);  
  
    }catch(Exception e) {
	e.printStackTrace();
    }finally{
	if(cursor != null){
	   cursor.close();
			}
	}
    return albumInfos;  
} 

在finally中关闭cursor,防止意外情况导致cursor没有关闭。

说到 cursor, 我处理时是直接把数据copy了一份,随时可用,当然,还有一种情况,就是直接查询直接用,这种情况通常会出现在使用 cursorAdapter中,这个时候我们不能再查询数据之后立刻关闭cursor, 因为这样会导致获取不到数据,而且CursorAdapt并没有再Activity退出时自动关闭,所以我们需要在onDestroy中去关闭cursor, 方法如下:

@Override
    protected void onDestroy() {
        super.onDestroy();
        if(mAdapter != null && mAdapter.getCursor() != null) {
            mAdapter.getCursor().close();
        }
    }


参考文章:

http://www.cnblogs.com/shang53880/archive/2012/05/18/2507943.html



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值