搜索手机上的所有文件和文件夹?

huangapple 未分类评论60阅读模式
标题翻译

Search All Files and Folders On Phone?

问题

我想要搜索所有文件和文件夹,以查找手机或平板电脑上的文档。似乎我只能搜索特定的目录,但我想要搜索所有pdf和docx文档的文件。类似于:

getLocal = (Button)findViewById(R.id.localFileButton);
getLocal.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        findFiles(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS));
    }
});

private void findFiles(File dir){
    //We could put these in an enumeration
    String pdfPattern = ".pdf";
    String docPattern = ".docx";
    String txtPattern = ".txt";

    File listFile[] = dir.listFiles();

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

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

                }
            }
        }
    }else{
        Toast.makeText(this, "No files on the phone", Toast.LENGTH_SHORT).show();
    }
}

我不想将搜索限制在一个文件夹中,我想要搜索任何可用的文件夹,有办法做到这一点吗?

英文翻译

I'm looking to search all files and folders for documents that are on a phone or tablet. Seems like I can only search certain directories, but want to search for all files for pdfs and docx documents. Something like:

getLocal = (Button)findViewById(R.id.localFileButton);
    getLocal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            findFiles(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS));
        }
    });
}

private void findFiles(File dir){
    //We could put these in an enumeration
    String pdfPattern = &quot;.pdf&quot;;
    String docPattern = &quot;.docx&quot;;
    String txtPattern = &quot;.txt&quot;;

    File listFile[] = dir.listFiles();

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

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

                }
            }
        }
    }else{
        Toast.makeText(this, &quot;No files on the phone&quot;, Toast.LENGTH_SHORT).show();
    }
}

}

I don't want to limit the search to just one folder, I want to search for any available folders, is there a way to do this?

huangapple
  • 本文由 发表于 2020年3月4日 04:59:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/60515424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定