Android 當媒體變動後,通知其餘應用從新掃描

 

在媒體文件改變後 發出 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE廣播,告知其餘應用,媒體文件發生改變。測試

 

具體代碼片斷:spa

 1 File oldFile = new File(oldPath);
 2 File newFile = new File(newPath);
 3 if (oldFile.exists() && !newFile.exists()) {
 4     if (oldFile.renameTo(newFile)) {
 5         
 6         Uri fileUri = Uri.fromFile(newFile);
 7         Intent intent = new Intent();
 8         intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
 9         intent.setData(fileUri);
10         sendBroadcast(intent);
11     }
12 }

通過測試上面的代碼在三星,聯想的手機上很差用code

通過試驗 將面代碼的 6- 10 行換成下面的代碼就好用了blog

sendBroadcast(newIntent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+Environment.getExternalStorageDirectory())));
相關文章
相關標籤/搜索