android 媒體資料庫重新整理

來源:互聯網
上載者:User

    android在啟動的時候會啟動MediaScannerService掃描系統上的多媒體檔案,然後將這些多媒體檔案的資訊加入到多媒體資料庫中,應用程式要取得這些多媒體資訊就是從這個多媒體資料庫裡面去取的,並不是從SD卡中取。也就是說,如果開機後增加或刪除了一些多媒體,這個多媒體資料庫是不會自動重新整理的。android提供了兩個Intent來發廣播讓系統自動重新整理多媒體資料庫,分別是Intent.ACTION_MEDIA_MOUNTED和Intent.Action_MEDIA_SCANNER_SCAN_FILE,前面的是掃描整個SD卡,後面的針對某個檔案進行掃描,發了Intent.ACTION_MEDIA_MOUNTED這個廣播後,還可以通過廣播接收器監聽ACTION_MEDIA_SCAN_STARTED和ACTION_MEDAI_SCAN_FINISH這兩個廣播,分別是開始掃描和掃描完畢時系統發出的。進行全卡掃描的話需要3-5秒的時間(我的情況),針對某個檔案掃描的沒有試過,呵呵,懶了。最近DLNA的DMC需要用到重新整理媒體庫的功能,不然之前一直是要是添加了新的檔案,就重啟手機,暈死了。網上搜尋了一趟,很多都是建議:


[java]
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
               Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath()))); 

 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath())));
可是我試了之後發現,每當我增加或減少了多媒體檔案後,我的整個音樂列表都不見了,不知道是哪裡出了原因。後來參考了以下兩位的做法,終於把功能實現了:

我的具體實現是:


[java]
public class MainActivity extends Activity { 
    private MediaScannerConnection mediaScanConn = null; 
    private MusicSannerClient client = null; 
    private File filePath = null; 
    private String fileType = null; 
 
    @SuppressLint("SdCardPath") 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.main); 
         
            client = new MusicSannerClient(); 
            mediaScanConn = new MediaScannerConnection(this, client); 
            scanfile(new File("/sdcard")); 
    } 
 
    class MusicSannerClient implements 
            MediaScannerConnection.MediaScannerConnectionClient { 
 
        public void onMediaScannerConnected() { 
            Log.e("---------", "media service connected"); 
 
            if (filePath != null) { 
 
                if (filePath.isDirectory()) { 
                    File[] files = filePath.listFiles(); 
                    if (files != null) { 
                        for (int i = 0; i < files.length; i++) { 
                            if (files[i].isDirectory()) 
                                scanfile(files[i]); 
                            else { 
                                mediaScanConn.scanFile( 
                                        files[i].getAbsolutePath(), fileType); 
                            } 
                        } 
                    } 
                } 
            } 
 
            filePath = null; 
 
            fileType = null; 
 
        } 
 
        public void onScanCompleted(String path, Uri uri) { 
            // TODO Auto-generated method stub  
            mediaScanConn.disconnect(); 
        } 
 
    } 
 
    private void scanfile(File f) { 
        this.filePath = f; 
        mediaScanConn.connect(); 
    } 
 

public class MainActivity extends Activity {
 private MediaScannerConnection mediaScanConn = null;
 private MusicSannerClient client = null;
 private File filePath = null;
 private String fileType = null;

 @SuppressLint("SdCardPath")
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.main);
  
   client = new MusicSannerClient();
   mediaScanConn = new MediaScannerConnection(this, client);
   scanfile(new File("/sdcard"));
 }

 class MusicSannerClient implements
   MediaScannerConnection.MediaScannerConnectionClient {

  public void onMediaScannerConnected() {
   Log.e("---------", "media service connected");

   if (filePath != null) {

    if (filePath.isDirectory()) {
     File[] files = filePath.listFiles();
     if (files != null) {
      for (int i = 0; i < files.length; i++) {
       if (files[i].isDirectory())
        scanfile(files[i]);
       else {
        mediaScanConn.scanFile(
          files[i].getAbsolutePath(), fileType);
       }
      }
     }
    }
   }

   filePath = null;

   fileType = null;

  }

  public void onScanCompleted(String path, Uri uri) {
   // TODO Auto-generated method stub
   mediaScanConn.disconnect();
  }

 }

 private void scanfile(File f) {
  this.filePath = f;
  mediaScanConn.connect();
 }

}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.