[转】MediaScanner用法总结

[转】MediaScanner用法总结

MediaScanner用法总结


转自这里以及这里


-   扫描全部

 

Java代码   收藏代码
  1. public void systemScan(){  
  2.         sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"  
  3.                 + Environment.getExternalStorageDirectory())));  
  4.     }  

 

 

-  扫描某个文件  参数:填入该文件的路径

 

Java代码   收藏代码
  1. public void fileScan(String file){  
  2.         Uri data = Uri.parse("file://"+file);  
  3.           
  4.         sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data));  
  5.     }  

 

 

- 扫描文件夹 参数:填入该文件夹路径

 

Java代码   收藏代码
  1. public void fileScan(String file){  
  2.         Uri data = Uri.parse("file://"+file);  
  3.           
  4.         sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data));  
  5.     }  
  6.       
  7.     public void folderScan(String path){  
  8.         File file = new File(path);  
  9.           
  10.         if(file.isDirectory()){  
  11.             File[] array = file.listFiles();  
  12.               
  13.             for(int i=0;i<array.length;i++){  
  14.                 File f = array[i];  
  15.                   
  16.                 if(f.isFile()){//FILE TYPE  
  17.                     String name = f.getName();  
  18.                       
  19.                     if(name.contains(".mp3")){  
  20.                         fileScan(f.getAbsolutePath());  
  21.                     }  
  22.                 }  
  23.                 else {//FOLDER TYPE  
  24.                     folderScan(f.getAbsolutePath());  
  25.                 }  
  26.             }  
  27.         }  
  28.     }  

 

 

 

 

你可能感兴趣的:([转】MediaScanner用法总结)