• Android调用MediaScanner进行扫描


      1 import android.content.Context;
      2 import android.media.MediaScannerConnection;
      3 import android.net.Uri;
      4 
      5 public class MediaScanner {
      6 
      7     private MediaScannerConnection mediaScanConn = null;
      8 
      9     private MusicSannerClient client = null;
     10 
     11     private String filePath = null;
     12     
     13     private String fileType = null;
     14     
     15     private String[] filePaths = null;
     16     /**
     17      * 然后调用MediaScanner.scanFile("/sdcard/2.mp3");
     18      * */
     19     public MediaScanner(Context context) {
     20         //创建MusicSannerClient
     21         if (client == null) {
     22 
     23             client = new MusicSannerClient();
     24         }
     25 
     26         if (mediaScanConn == null) {
     27 
     28             mediaScanConn = new MediaScannerConnection(context, client);
     29         }
     30     }
     31 
     32     class MusicSannerClient implements
     33             MediaScannerConnection.MediaScannerConnectionClient {
     34 
     35         public void onMediaScannerConnected() {
     36             
     37             if(filePath != null){
     38                 
     39                 mediaScanConn.scanFile(filePath, fileType);
     40             }
     41             
     42             if(filePaths != null){
     43                 
     44                 for(String file: filePaths){
     45                     
     46                     mediaScanConn.scanFile(file, fileType);
     47                 }
     48             }
     49             
     50             filePath = null;
     51             
     52             fileType = null;
     53             
     54             filePaths = null;
     55         }
     56 
     57         public void onScanCompleted(String path, Uri uri) {
     58             // TODO Auto-generated method stub
     59             mediaScanConn.disconnect();
     60         }
     61 
     62     }
     63     
     64     /**
     65      * 扫描文件标签信息
     66      * @param filePath 文件路径 eg:/sdcard/MediaPlayer/dahai.mp3
     67      * @param fileType 文件类型 eg: audio/mp3  media/*  application/ogg
     68      * */
     69     public void scanFile(String filepath,String fileType) {
     70 
     71         this.filePath = filepath;
     72         
     73         this.fileType = fileType;
     74         //连接之后调用MusicSannerClient的onMediaScannerConnected()方法
     75         mediaScanConn.connect();
     76     }
     77     /**
     78      * @param filePaths 文件路径
     79      * @param fileType 文件类型
     80      * */
     81     public void scanFile(String[] filePaths,String fileType){
     82         
     83         this.filePaths = filePaths;
     84         
     85         this.fileType = fileType;
     86         
     87         mediaScanConn.connect();
     88         
     89     }
     90     
     91     public String getFilePath() {
     92 
     93         return filePath;
     94     }
     95 
     96     public void setFilePath(String filePath) {
     97 
     98         this.filePath = filePath;
     99     }
    100 
    101     public String getFileType() {
    102         
    103         return fileType;
    104     }
    105 
    106     public void setFileType(String fileType) {
    107         
    108         this.fileType = fileType;
    109     }
    110 
    111     
    112 }
  • 相关阅读:
    REVERSE-Daily(4)-Elfcrackme2
    DES带IV向量加密解密工具
    Reverse-Daily(3)-DotNetCrackMe1
    Reverse-Daily(2)-wow
    C#:using和new关键字
    python3实现UDP协议的简单服务器和客户端
    python3实现TCP协议的简单服务器和客户端
    深入C++的运算符重载
    NLTK学习笔记(五):分类和标注词汇
    NLTK学习笔记(四):自然语言处理的一些算法研究
  • 原文地址:https://www.cnblogs.com/androidxiaoyang/p/3068395.html
Copyright © 2020-2023  润新知