来自 IT技术新闻 2019-01-18 09:08 的文章

android 媒体数据库刷新

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

}

 

http://www.bkjia.com/Androidjc/538881.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/538881.htmlTechArticleandroid在启动的时候会启动MediaScannerService扫描系统上的多媒体文件,然后将这些多媒体文件的信息加入到多媒体数据库中,应用程序要取得...

本文源自: AG88平台