Android如何通知MediaScanner扫描出指定文件

Posted 邱小琪Yogi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android如何通知MediaScanner扫描出指定文件相关的知识,希望对你有一定的参考价值。

作者:Yogi

前言:如果要扫描u盘的话,通过发送广播的方式,则会扫描整个u盘,若文件多,那是相当耗时的。这里教你如何扫描指定的文件夹,若知道文件名,当然也可以扫描指定文件。扫描之后,我们就可以在媒体数据库查询到新添加的文件了。

#原理
用MediaScannerConnectionClient来通知MediaScanner更新单个文件,不需要用广播的方式,也就不需要系统权限(android:sharedUserId=“android.uid.system”)了。

#代码

/**
     * notify media scanner to update database.
     * @author yogi
     * @version 1.0.0
     */
    final class MyMediaScannerConnectionClient implements MediaScannerConnectionClient 
        /**
         * file name of your first time,because when you invoke this,media scanner connection may not connected,when it connected,it will invoke onMediaScannerConnected() automatically.
         */
        private String mFilename = null;
        /**
         * service connection to media scanner.
         */
        private MediaScannerConnection mConn = null;

        /**
         * constructor.
         * @param ctx context.
         * @param file file name.
         */
        public MyMediaScannerConnectionClient (final Context ctx, final String file) 
            this.mFilename = file;
            mConn = new MediaScannerConnection(ctx, this);
            Log.i(TAG, "MyMediaScannerConnectionClient constructor,connect service here");
            mConn.connect();
        

        /**
         * scan file,it can be directory or single file.
         * @param fileName fileName,if it's a directory,then get its every file.
         */
        public void scanFile(final String fileName) 
            Log.i(TAG, "scanFile fileName=" + fileName);
            if (TextUtils.isEmpty(fileName)) 
                Log.w(TAG, "scanFile fileName empty!!!");
                return;
            
            File f = new File(fileName);
            if (!f.exists()) 
                Log.w(TAG, "scanFile this file does not exist!!!");
                return;
            
            if ((mConn != null)
                    && mConn.isConnected()) 
                if (f.isDirectory()) 
                    File[] filesInThisDir = f.listFiles();
                    if ((filesInThisDir != null)
                            && (filesInThisDir .length > 0)) 
                        for (File file : filesInThisDir) 
                            String filePath = file.getAbsolutePath();
                            Log.i(TAG, "scanFile directory file:getAbsolutePath=" + filePath);

                                scanFile(file.getAbsolutePath());
                           
                        
                    
                    Log.i(TAG, "scanFile it's a directory,now scan its files done");
                 else 
                    mConn.scanFile(fileName, null);
                    Log.i(TAG, "scanFile the file done");
                
            
        

        @Override
        public void onMediaScannerConnected() 
            Log.i(TAG, "onMediaScannerConnected  mFilename= " + mFilename);
            scanFile(mFilename);
            Log.i(TAG, "onMediaScannerConnected scanFile done");
        

        @Override
        public void onScanCompleted(final String path, final Uri uri) 
            Log.i(TAG, "onScanCompleted  scan path=" + path
                    + " uri= " + uri);
            //            mConn.disconnect();
        

    

以上是关于Android如何通知MediaScanner扫描出指定文件的主要内容,如果未能解决你的问题,请参考以下文章

MediaScanner-MediaPrivider-插拔U盘多媒体扫描并优化

[深入理解Android卷一全文-第十章]深入理解MediaScanner

mediascanner流程

Android MediaScanner 总纲

Android多媒体分析MediaScanner

Android MediaScannerJNI源代码具体解释