最近碰巧看到 Android Police 在抱怨有關 Kitkat (4.4.2) 的媒體掃描功能
拿自己的 code 來用的時候發現舊方法不能 work
而且沒用就算了…還會跳 Exception XDDDD
java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED
拜了大神馬上就看到 stackoverflow 上有人提到
不過似乎只能逐一更新
寫了一個簡單但不是很好的解法 Orz
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
private void scanDirKK(String path) { try { File payload = new File(path); if(payload.isDirectory()) { ScanTask task = new ScanTask(); task.execute(path); } else scanFile(new File(path)); } catch (Exception e) { Log.w(tag, "scan for kk device failed, " + e.getMessage()); } } class ScanTask extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { waitDialog = ProgressDialog.show(MediaScan.this, "", "掃描中..."); } @Override protected String doInBackground(String... arg) { try { File[] dir = new File(arg[0]).listFiles(); for(File file : dir) scanFile(file); } catch (Exception e) { Log.w(tag, "scan folder failed, " + e.getMessage()); } return null; } @Override public void onPostExecute(String result) { if(waitDialog!=null && waitDialog.isShowing()) waitDialog.dismiss(); } } private void scanFile(File file) { MediaScannerConnection.scanFile(this,new String[] { file.toString() }, null, new MediaScannerConnection.OnScanCompletedListener() { public void onScanCompleted(String path, Uri uri) { Log.i(tag, "ExternalStorage Scanned " + path + ":"); Log.i(tag , "ExternalStorage -> uri=" + uri); } }); } |