使用android查找文件夹中的文件

时间:2015-07-14 18:17:01

标签: android

我想和in this question做同样的事情,但在Android上,我想返回完整的文件路径如果在EditText中它存在= true? 我如何以及在何处放置代码

EditText txt11 = (EditText) findViewById(R.id.editText11);
txt11.setText(ss2 , TextView.BufferType.EDITABLE);

获取完整文件路径

// your directory
File f = new File("C:\\example");
File[] matchingFiles = f.listFiles(new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return name.startsWith("temp") && name.endsWith("txt");
    }
});

1 个答案:

答案 0 :(得分:0)

感谢所有我得到了我想要的东西。 祝福

        File f = new File(Environment.getExternalStorageDirectory().getPath() + "/image1/Original_Images/" );
        getAllFilesOfDir(f);
}

    private void getAllFilesOfDir(File directory) {
        EditText txt12 = (EditText) findViewById(R.id.editText12);
        EditText txt10 = (EditText) findViewById(R.id.editText10); // We have this IN  editText10 = "_20151.jpg"
                                     //  /storage/sdcard0/image1/Original_Images/TARIQ_20151.jpg"  We want to find TARIQ
        final File[] files = directory.listFiles();

        if ( files != null ) {
            for ( File file : files ) {
                if ( file != null ) {
                    if ( file.isDirectory() ) {  // it is a folder...
                        getAllFilesOfDir(file);
                    }
                    else {  // it is a file...
                      String  str2=file.getAbsolutePath();
                        String  str3=file.getAbsolutePath();

                        int start = str2.toString().trim(). lastIndexOf("_");
                        String suffix =  str2.toString().trim().substring(start );


if(suffix.toString().trim().equals(txt10.getText().toString().trim())){

    int start2 = str3.toString().trim(). lastIndexOf("/");
    int end2 = str3.toString().trim(). lastIndexOf("_");
    String suffix2 =  str2.toString().trim().substring(start2+1,end2);


    txt12.setText( suffix2.toString().trim() , TextView.BufferType.EDITABLE);
return;


}    else { txt12.setText(null  , TextView.BufferType.EDITABLE);}


                    }
                }
            }
        }
    }

}