6

我正在开发一个项目,该项目包含一个模块,用于从 sd 卡扫描 pdf、doc xls 文件并列出它们。我也想只列出文件夹。

因为我对android很陌生。任何人都有实现这一目标的想法。

这是我的代码:

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
    }
}
4

4 回答 4

23
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

              }
            }
        }
    }    }

要搜索整个 sdcard,请使用此函数walkdir(Environment.getExternalStorageDirectory());

于 2012-12-10T12:22:23.663 回答
2

使用getExternalStorageDirectory()获取 SD 卡路径。(不要硬编码)

遍历每个子文件夹,并检查具有所需扩展名的文件名。使用 String endswith()方法检查文件名是否以扩展名结尾。

这是一个可能对您有所帮助的示例代码。

于 2012-12-10T12:24:05.633 回答
2

我建议使用 commons.io处理符号链接和扩展解析的库。

扫描带有扩展名的文件的任务:

import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ScanTask extends AsyncTask<String,Void,ScanTask.Result> {

    @Override
    protected Result doInBackground(String... extensions) {

        if(extensions == null){
            extensions = new String[0];
        }

        List<File> files = new ArrayList<File>();
        boolean success = false;
        String status = "";

        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            try {
                files.addAll(FileUtils.listFiles(Environment.getExternalStorageDirectory(), extensions, true));
                success = true;
                status = "ok";
            } catch (Exception e) {
                Log.e("MyApp","File error:",e);
                success = false;
                status = "Scan error";
            }
        }else {
            success = false;
            status = "External storage not available";
        }


        return new Result(success,status,files);
    }

    public static class Result{
        public final boolean success;
        public final String message;
        public final List<File> files;

        public Result(boolean success, String message,List<File> files) {
            this.success = success;
            this.message = message;
            this.files = files;
        }
    }
}

用法:

   ScanTask task = new ScanTask(){
        @Override
        protected void onPostExecute(Result result) {
            super.onPostExecute(result);

            if(result.success){
                List<File> files = result.files;
                //--do something with files--
            }else {
                String error = result.message;
                //--do something with error message--
            }
        }
    };

    task.execute("mp3","jpeg");
于 2013-08-02T08:08:19.433 回答
0

您可以使用此代码:
这是链接

/**
  * Class to filter files which are having .mp3 extension
  * */
//you can choose the filter for me i put .mp3
class FileExtensionFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".pdf" ) || name.endsWith(".docs" )) || name.endsWith(".3gp" ) ;
    }
}
于 2015-10-24T04:47:28.900 回答