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()); } } } }