zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    C#延时
    Java Applet 绘图
    C#调用ActiveX控件异常处理
    J2SE连连看
    基于对象的棋类程序结构设计
    MATLAB应用专题part2电力电子仿真技术
    MATLAB应用专题part1电力电子仿真技术
    硬件知识整理part2电阻在反馈网络中的应用
    C语言再学习part1宏观认识C语言
    C语言再学习part3—算法
  • 原文地址:https://www.cnblogs.com/androidxiaoyang/p/3068395.html
Copyright © 2011-2022 走看看