如何扫描SD卡的文件在android的特定扩展名? [英] How to scan SD card for particular extension of files in android?

查看:212
本文介绍了如何扫描SD卡的文件在android的特定扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,其中包含一个模块从SD卡扫描PDF,DOC XLS文件,使他们的列表中选择一个项目。我还希望文件夹列表而已。

由于我很新的Andr​​oid系统。任何人有做到这一点的想法。

下面是我的code:

 公共类MediaScannerWrapper工具
MediaScannerConnection.MediaScannerConnectionClient {
    私人MediaScannerConnection mConnection;
    私人字符串的mpath;
    私人字符串mMimeType;

    //文件路径 - 在扫描;
    //媒体MIME类型来扫描,即为image / jpeg。
    //使用* / *的任何媒体
    公共MediaScannerWrapper(上下文CTX,文件路径字符串,字符串MIME){
        的mpath =文件路径;
        mMimeType =哑剧;
        mConnection =新MediaScannerConnection(CTX,这一点);
    }

    //做扫描
    公共无效扫描(){
        mConnection.connect();
    }

    //开始扫描时,扫描仪准备就绪
    公共无效onMediaScannerConnected(){
        mConnection.scanFile(的mpath,mMimeType);
        Log.w(MediaScannerWrapper,媒体文件扫描:+的mpath);
    }

    公共无效onScanCompleted(字符串路径,开放的URI){
        //当扫描完成后,更新的媒体文件标签
    }
}
 

解决方案

 公共无效walkdir(文件目录){
    字符串pdfPattern =.PDF;

    文件[]的文件列表= dir.listFiles();

    如果(的文件列表!= NULL){
        的for(int i = 0; I< listFile.length;我++){

            如果(listfile中[I] .isDirectory()){
                walkdir(的文件列表[I]);
            } 其他 {
              如果(的文件列表[I] .getName()的endsWith(pdfPattern)){
                              //做什么都ü希望

              }
            }
        }
    }}
 

要使用 walkdir对整个SD卡电话本搜索功能(Environment.getExternalStorageDirectory());

I am working on a project which contains a module to scan pdf, doc xls files from sd card and make list of them. I also want to make list of folders only.

As I am very new to android. Anyone have idea of achieving this.

Here is my code:

public class MediaScannerWrapper implements  
MediaScannerConnection.MediaScannerConnectionClient {
    private MediaScannerConnection mConnection;
    private String mPath;
    private String mMimeType;

    // filePath - where to scan; 
    // mime type of media to scan i.e. "image/jpeg". 
    // use "*/*" for any media
    public MediaScannerWrapper(Context ctx, String filePath, String mime){
        mPath = filePath;
        mMimeType = mime;
        mConnection = new MediaScannerConnection(ctx, this);
    }

    // do the scanning
    public void scan() {
        mConnection.connect();
    }

    // start the scan when scanner is ready
    public void onMediaScannerConnected() {
        mConnection.scanFile(mPath, mMimeType);
        Log.w("MediaScannerWrapper", "media file scanned: " + mPath);
    }

    public void onScanCompleted(String path, Uri uri) {
        // when scan is completes, update media file tags
    }
}

解决方案

public void walkdir(File dir) {
    String pdfPattern = ".pdf";

    File[] listFile = dir.listFiles();

    if (listFile != null) {
        for (int i = 0; i < listFile.length; i++) {

            if (listFile[i].isDirectory()) {
                walkdir(listFile[i]);
            } else {
              if (listFile[i].getName().endsWith(pdfPattern)){
                              //Do what ever u want

              }
            }
        }
    }    }

To search on the whole sdcard call this function usingwalkdir(Environment.getExternalStorageDirectory());

这篇关于如何扫描SD卡的文件在android的特定扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆