android - 如何在 Android 中扫描 SD 卡以查找文件的特定扩展名?

标签 android sd-card documents

我正在开发一个项目,其中包含一个模块,用于从 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
    }
}

最佳答案

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

              }
            }
        }
    }    }

要搜索整个 SD 卡,请使用 walkdir(Environment.getExternalStorageDirectory());

调用此函数

关于android - 如何在 Android 中扫描 SD 卡以查找文件的特定扩展名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13800667/

相关文章:

android - Chrome 无法从 Android 应用打开

android - android应用程序如何检测设备中的左右SD卡

mongodb - 替换 MongoDB 中数组中的嵌入文档

android - Flutter中RelativeLayout的等价物

android - 从 Jenkins 传递 MAVEN 配置文件参数

android - React native 生成​​ ProGuard 映射文件

mongodb - 对于最有效地查询用户关注者/被关注者来说,什么是好的 MongoDB 文档结构?

android - 在 Android 中的 DCIM 文件夹下的 SD 卡上创建文件夹时出错

Android:从SD卡中文件的Uri获取文件路径

ms-word - 在 Word 中禁用文档模板链接