android媒體檔案掃描

來源:互聯網
上載者:User

標籤:

  項目中可能有這樣的需求:下載或匯入、匯出的圖片、音樂等媒體檔案,需要馬上能在圖庫或本地視屏播放器中顯示出來,或者要能在媒體資料庫中查詢到媒體檔案的相關資訊,這時我們就得主動通知系統掃描新的媒體檔案了。我整理了一個掃描的工具類,分享下。

  具體代碼:

public class MediaScanner {    private volatile static MediaScanner instance;    private MediaScanner(){    }    public static MediaScanner getInstace(){        synchronized (MediaScanner.class){            if(instance == null){                instance = new MediaScanner();            }        }        return instance;    }    /**     * 掃描一個媒體檔案     * @param filePath 要掃描的媒體檔案     */    public void scanFile(Context context, ScanFile filePath) {        List<ScanFile> filePaths = new ArrayList<ScanFile>(1);        filePaths.add(filePath);        scanFiles(context, filePaths);    }    /**     * 掃描多個媒體檔案     * @param filePaths 要掃描的檔案清單     */    public void scanFiles(Context context, List<ScanFile> filePaths){        SannerClient client = new SannerClient(context, filePaths);        client.connectAndScan();    }    /**     * 媒體檔案掃描物件建構器     */    public static class ScanFile{        /**         * 要掃描的媒體檔案路勁或包含媒體檔案的檔案夾路徑         */        public String filePaths;        /**         * 要掃描的媒體檔案類型 eg: audio/mp3  media/*  application/ogg         *             image/jpeg  image/png  video/mpeg   video/3gpp         *             ......         */        public String mineType;        public ScanFile(String filePaths, String mineType) {            this.filePaths = filePaths;            this.mineType = mineType;        }    }    public class SannerClient implements            MediaScannerConnection.MediaScannerConnectionClient {        /**         * 要掃描的檔案或檔案夾         */        private List<ScanFile> scanFiles = null;        /**         * 實際要掃描的單個檔案集合         */        private List<ScanFile> filePaths = null;        private MediaScannerConnection mediaScanConn;        public SannerClient(Context context, List<ScanFile> scanFiles) {            this.scanFiles = scanFiles;            mediaScanConn = new MediaScannerConnection(context, this);        }        /**         * 串連MediaScanner並開始掃描         */        public void connectAndScan(){            if(scanFiles != null && !scanFiles.isEmpty()){                this.filePaths = new ArrayList<ScanFile>();                //遍曆取得單個檔案集合                for(ScanFile sf : scanFiles){                    findFile(sf);                }                mediaScanConn.connect();            }        }        private void findFile(ScanFile file){            File f = new File(file.filePaths);            if(f.isFile()){                filePaths.add(file);            }else{                File[] fs = f.listFiles();                if(fs != null && fs.length > 0){                    for(File cf : fs){                        findFile(new ScanFile(cf.getAbsolutePath(), file.mineType));                    }                }            }        }        private void scanNext(){            if(filePaths != null && !filePaths.isEmpty()){                ScanFile sf = filePaths.remove(filePaths.size() - 1);                mediaScanConn.scanFile(sf.filePaths, sf.mineType);            }else{                mediaScanConn.disconnect();            }        }        @Override        public void onMediaScannerConnected() {            scanNext();        }        @Override        public void onScanCompleted(String path, Uri uri) {            // TODO Auto-generated method stub            scanNext();            //媒體掃描完成可以配合EventBus等訊息通訊工具發出通知,也可接收Intent.ACTION_MEDIA_SCANNER_FINISHED的廣播            //EventBus.getDefault().post(new EventMediaScanCompleted(path));        }    }}

 

  如果有掃描問題歡迎反饋。

android媒體檔案掃描

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.