android 扫描文件(sdcard添加新的音乐文件时候后,可扫描到)

如果在sdcard中添加了新的音乐,我们想要把心得音乐添加进listview中,

一种是sendbroadcast()发一个intent给系统,让系统扫描整个sdcard,但是据说这个在android4.4(具体几点几记不清了,可以上网一查)不能使用,我在使用模拟器的时候同样报错,真机调试没问题,或许是真机系统版本低的原因

二是调用MediaScannerConnection.scanFile(context, new String[] {file.getAbsolutePath() }, 
null, new MyOnScanCompletedListener(i,files.length));

后面那个参数是我自定义的一个类,继承了OnScanCompletedListener接口

private class MyOnScanCompletedListener implements OnScanCompletedListener{

		private int index ;
		private int fileNum;
		public MyOnScanCompletedListener(int index,int fileName) {
			// TODO Auto-generated constructor stub
			this.index = index;
			this.fileNum = fileName;
		}
		
		
		@Override
		public void onScanCompleted(String path, Uri uri) 
        {
//            Log.i("GetNativeMusicThread", "Scanned " + path + ":");
            Log.i("GetNativeMusicThread", "-> uri=" + uri);
			
          Log.i("GetNativeMusicThread", "file num is : "+fileNum+" ,index:"+index);
	      
//			
//			String[] proj = { MediaStore.Images.Media.DATA };
//			Cursor actualimagecursor = context.managedQuery(uri,proj,null,null,null);
//			int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
//			actualimagecursor.moveToFirst();
//			String img_path = actualimagecursor.getString(actual_image_column_index);
//			File file = new File(img_path);
//			Uri fileUri = Uri.fromFile(file);
			
			
			
			Cursor cursor =context.getContentResolver().query(
					uri, null, null, null,
					MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
//			List<MusicInfo> musicInfos = new ArrayList<MusicInfo>();
//	        Log.e("GetNativeMusicThread", "getmusicinfos+ "+cursor.getCount()+",,,uri :"+MediaStore.Audio.Media.EXTERNAL_CONTENT_URI); 
			if (cursor != null) {
//				for (int i = 0; i < cursor.getCount(); i++) {
				while(cursor.moveToNext()){
					MusicInfo musicInfo = new MusicInfo();
//					cursor.moveToNext();
					long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)); //音乐id  
					String title = cursor.getString((cursor.getColumnIndex(MediaStore.Audio.Media.TITLE))); //音乐标题  
					String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));  //艺术家 
					long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));  //时长
					long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));  //文件大小 
					String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));   //文件路径 
					int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));  //是否为音乐  
					long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); //albumid  
					System.out.println("in all url: "+ url);
	                Log.i("GetNativeMusicThread", "Scanned  title " +",getmusicinfos: "+cursor.getCount()+title+",artist :"+artist+",,size:"+size +",,isMusic:"+isMusic);
					if (isMusic != 0) {//只把音乐添加到集合当中 
						musicInfo.setId(id);
						musicInfo.setTitle(title);
						musicInfo.setArtist(artist);
						musicInfo.setDuration(duration);
						musicInfo.setUrl(url);
						musicInfo.setSize(size);
						musicInfo.setAlbumId(albumId);
						musicInfos.add(musicInfo);
						
					}
				}
				cursor.close();
				//扫描到10个文件或者到文件末尾就sendmessage
				if ((index+1) %10 == 0 || (index+1) == fileNum) {

					//将音乐文件的信息对象发送出去
//		          	musicInfos = getMusicInfos(context);
//	    			handler.obtainMessage(CONSTANTS.MSG_GET_NATIVE_MUSIC_SUCCESS, musicInfos).sendToTarget();//这个没想到怎么延时
	    			Message msg = handler.obtainMessage(CONSTANTS.MSG_GET_NATIVE_MUSIC_SUCCESS, musicInfos);
	    			handler.sendMessageDelayed(msg, 1000);//延时5秒再发送消息
	    			
				}
				
			}
			
//			System.out.println("---------"+musicInfos.size());

        }  
      
		
	}

这里有一点很奇怪,前几天我用的时候这样扫描之后的音乐信息并不会添加到数据库中去,也就是说不能通过数据库(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI)来查找该音乐的信息,

但是今天突然就可以了,不知道为什么,,,,,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值