4.Java 层扫描文件 java
a ) Java 层开始文件 android
首先它忽略一些 MacOS 和 Windows Media Player 特殊的文件。而后它会查看被扫描的文件是否已经存在于缓存条目中,若是存在,它会检查文件上次修改的时间是否改变。最后它返回该文件是否须要进一步处理的结果。若是不须要,接下来的两步不会执行。 git
b)C++ 层扫描文件 数据库
不是全部的文件都须要交给 C++ 层解析成元数据。只有下面的文件类型会被解析,注意,这里不处理 image 文件。 缓存
对于被解析的元数据信息, C++ 层会回调到 JAVA 层的 handleStringTag 。 Java 层会记录它的 name/value 信息。 网络
c)Java 层结束文件 app
最后根据上一步解析出的值, Java 层会更新相应的 MeidaProvider 产生的数据库表。 ide
5.Java 层发送扫描 工具
到目前为止,全部文件已经被扫描,它最后会检查文件和播放列表缓存条目,看是否全部项仍然存在于文件系统。若是有空条目,则会从数据库中删除。这样它可以保持数据库和文件系统的一致性。 url
其余的应用 程序 经过接收 MediaScannerService 发出的 ACTION_MEDIA_SCANNER_STARTED 和 ACTION_MEDIA_SCANNER_FINISHED 意图可以知道何时扫描操做开始和结束。
MediaScanner
之因此拿MediaScanner开刀 由于想借用系统的Media Scan 工具 经过Intent直接调用系统的
[步骤]
1. 下载并安装Git 过程略 网络上不少
2. 获得该功能的模块地址并使用Git下载之 地址:git://android.git.kernel.org/platform/packages/providers/MediaProvider.git
3. 分析源代码:
- AndroidManifest.xml : 各组件属性描述文件
- MediaProvider : extends ContentProvider 使用SQLiteDatabase 保存查询数据 action="content://media"
- MediaScannerCursor.java
- MediaScannerReceiver : extends BroadcastReceiver 用于接收指定Broadcast: BOOT_COMPLETED MEDIA_MOUNTED MEDIA_SCANNER_SCAN_FILE 并启动 MediaScannerService 开始扫描
- MediaScannerService : extends Service 执行具体的扫描工做
- MediaThumbRequest
4. 鉴于 并不打算自行实现多媒体扫描 所以 这次重点研究对象:MediaScannerReceiver
public class MediaScannerReceiver extends BroadcastReceiver { private final static String TAG = "MediaScannerReceiver" ; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Uri uri = intent.getData(); String externalStoragePath = Environment.getExternalStorageDirectory().getPath(); if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { // scan internal storage scan(context, MediaProvider.INTERNAL_VOLUME); } else { if (uri.getScheme().equals( "file" )) { // handle intents related to external storage String path = uri.getPath(); if (action.equals(Intent.ACTION_MEDIA_MOUNTED) && externalStoragePath.equals(path)) { scan(context, MediaProvider.EXTERNAL_VOLUME); } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) && path != null && path.startsWith(externalStoragePath + "/" )) { scanFile(context, path); } } } } private void scan(Context context, String volume) { Bundle args = new Bundle(); args.putString("volume" , volume); context.startService( new Intent(context, MediaScannerService. class ).putExtras(args)); } private void scanFile(Context context, String path) { Bundle args = new Bundle(); args.putString("filepath" , path); context.startService( new Intent(context, MediaScannerService. class ).putExtras(args)); } } Java代码 public class MediaScannerReceiver extends BroadcastReceiver { private final static String TAG = "MediaScannerReceiver"; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Uri uri = intent.getData(); String externalStoragePath = Environment.getExternalStorageDirectory().getPath(); if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { // scan internal storage scan(context, MediaProvider.INTERNAL_VOLUME); } else { if (uri.getScheme().equals("file")) { // handle intents related to external storage String path = uri.getPath(); if (action.equals(Intent.ACTION_MEDIA_MOUNTED) && externalStoragePath.equals(path)) { scan(context, MediaProvider.EXTERNAL_VOLUME); } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) && path != null && path.startsWith(externalStoragePath + "/")) { scanFile(context, path); } } } } private void scan(Context context, String volume) { Bundle args = new Bundle(); args.putString("volume", volume); context.startService( new Intent(context, MediaScannerService.class).putExtras(args)); } private void scanFile(Context context, String path) { Bundle args = new Bundle(); args.putString("filepath", path); context.startService( new Intent(context, MediaScannerService.class).putExtras(args)); } } public class MediaScannerReceiver extends BroadcastReceiver { private final static String TAG = "MediaScannerReceiver"; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Uri uri = intent.getData(); String externalStoragePath = Environment.getExternalStorageDirectory().getPath(); if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { // scan internal storage scan(context, MediaProvider.INTERNAL_VOLUME); } else { if (uri.getScheme().equals("file")) { // handle intents related to external storage String path = uri.getPath(); if (action.equals(Intent.ACTION_MEDIA_MOUNTED) && externalStoragePath.equals(path)) { scan(context, MediaProvider.EXTERNAL_VOLUME); } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) && path != null && path.startsWith(externalStoragePath + "/")) { scanFile(context, path); } } } } private void scan(Context context, String volume) { Bundle args = new Bundle(); args.putString("volume", volume); context.startService( new Intent(context, MediaScannerService.class).putExtras(args)); } private void scanFile(Context context, String path) { Bundle args = new Bundle(); args.putString("filepath", path); context.startService( new Intent(context, MediaScannerService.class).putExtras(args)); } }
6. 根据以上代码得知:
- 当系统启动完毕 会扫描一次
- 当 ACTION_MEDIA_MOUNTED ACTION_MEDIA_SCANNER_SCAN_FILE 也会扫描
7. 如何调用系统MediaScanner 进行扫描
- 经过 Intent.ACTION_MEDIA_MOUNTED 进行全扫描
public void allScan(){ sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse( "file://" + Environment.getExternalStorageDirectory()))); }
Java代码
public void allScan(){ sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); }经过 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE 扫描某个文件
public void fileScan(String fName){ Uri data = Uri.parse("file:///" +fName); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data)); }
public void fileScan(String fName){ Uri data = Uri.parse("file:///"+fName); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data)); }
补充: 上述方法是不支持对文件夹的 即:Uri data 必须是 文件的Uri 若是是文件夹的 其不会起做用的 切记!
- 如何扫描某文件夹下全部文件 难道就不能够么? 固然不 借助于Intent.ACTION_MEDIA_SCANNER_SCAN_FILE
咱们能够这么作: 取出该文件夹下的全部子文件 如其是文件且类型符合条件 就取出该文件目录 以 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE方式发送至MediaScannerReceiver 若其为文件夹 则迭代查询之 故实现为:
public void fileScan(String file){ Uri data = Uri.parse("file://" +file); Log.d("TAG" , "file:" +file); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data)); } public void folderScan(String path){ File file = new File(path); if (file.isDirectory()){ File[] array = file.listFiles(); for ( int i= 0 ;i<array.length;i++){ File f = array[i]; if (f.isFile()){ //FILE TYPE String name = f.getName(); if (name.contains( ".mp3" )){ fileScan(f.getAbsolutePath()); } } else { //FOLDER TYPE folderScan(f.getAbsolutePath()); } } } }
public void fileScan(String file){ Uri data = Uri.parse("file://"+file); Log.d("TAG","file:"+file); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data)); } public void folderScan(String path){ File file = new File(path); if(file.isDirectory()){ File[] array = file.listFiles(); for(int i=0;i<array.length;i++){ File f = array[i]; if(f.isFile()){//FILE TYPE String name = f.getName(); if(name.contains(".mp3")){ fileScan(f.getAbsolutePath()); } } else {//FOLDER TYPE folderScan(f.getAbsolutePath()); } } } }
8. 鉴于多数人并不关心其原理 仅关系如何使用 故 总结以下:
- 扫描所有 我猜想其在效率方面可能有点反作用
public void systemScan(){ sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse( "file://" + Environment.getExternalStorageDirectory()))); }
public void systemScan(){ sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); }扫描某个文件 参数:填入该文件的路径
public void fileScan(String file){ Uri data = Uri.parse("file://" +file); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data)); }
public void fileScan(String file){ Uri data = Uri.parse("file://"+file); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data)); }
- 扫描文件夹 参数:填入该文件夹路径
public void fileScan(String file){ Uri data = Uri.parse("file://" +file); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data)); } public void folderScan(String path){ File file = new File(path); if (file.isDirectory()){ File[] array = file.listFiles(); for ( int i= 0 ;i<array.length;i++){ File f = array[i]; if (f.isFile()){ //FILE TYPE String name = f.getName(); if (name.contains( ".mp3" )){ fileScan(f.getAbsolutePath()); } } else { //FOLDER TYPE folderScan(f.getAbsolutePath()); } } } }
public void fileScan(String file){ Uri data = Uri.parse("file://"+file); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data)); } public void folderScan(String path){ File file = new File(path); if(file.isDirectory()){ File[] array = file.listFiles(); for(int i=0;i<array.length;i++){ File f = array[i]; if(f.isFile()){//FILE TYPE String name = f.getName(); if(name.contains(".mp3")){ fileScan(f.getAbsolutePath()); } } else {//FOLDER TYPE folderScan(f.getAbsolutePath()); } } } } public void fileScan(String file){ Uri data = Uri.parse("file://"+file); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data)); } public void folderScan(String path){ File file = new File(path); if(file.isDirectory()){ File[] array = file.listFiles(); for(int i=0;i<array.length;i++){ File f = array[i]; if(f.isFile()){//FILE TYPE String name = f.getName(); if(name.contains(".mp3")){ fileScan(f.getAbsolutePath()); } } else {//FOLDER TYPE folderScan(f.getAbsolutePath()); } } } }