This example for you to share the Android traverse all folders and subdirectories to search for files for your reference, the specific content as follows
Java code:
Import Java.io.File;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.TextView;
Import Android.widget.Toast;
public class Shuosouwenjianactivity extends activity implements Onclicklistener {private file file;
Private String path;
Private String info; Private String key; Keywords private TextView result; Display results private edittext et; Edit View private Button search_btn; Button view @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (Savedinstancesta
TE);
Setcontentview (R.layout.main);
result = (TextView) Findviewbyid (R.id.textview_result);
ET = (edittext) Findviewbyid (R.id.key);
SEARCH_BTN = (Button) Findviewbyid (R.id.button_search);
File = new file (Environment.getexternalstoragedirectory (). GetPath ());
File = new file ("/sdcard/"); info = getString (r.string.info);
Search_btn.setonclicklistener (this);
@Override public void OnClick (View v) {//TODO auto-generated Method Stub path = "";
Result.settext ("");
Key = Et.gettext (). toString ();
Browserfile (file); } public void Browserfile (File fileold) {if (Key.equals (")) {Toast.maketext (this, getString (r.string
. Pleaseinput), Toast.length_long). Show ();
else {search (fileold);
if (Result.gettext (). Equals ("")) {Toast.maketext (this, getString (R.string.notfound), Toast.length_short). Show ();
}} private void Search (File fileold) {try{file[] files=fileold.listfiles (); if (files.length>0) {for (int j=0;j<files.length;j++) {if (!files[j].isdirectory ()) {if (Files[j].
GetName (). IndexOf (Key) >-1) {path + = "\ n" + files[j].getpath ();
Result.settext (Info+path); Shuju.putstring (Files[j].getname (). tOstring (), Files[j].getpath (). toString ());
} else{This.search (Files[j]);
catch (Exception e) {}}}
MAIN. XML code:
<?xml version= "1.0" encoding= "Utf-8"?> <absolutelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
android:orientation=" vertical "
android:layout_width=" fill_parent "
android:layout_" height= "Fill_parent"
android:id= "@+id/widget0"
>
<button android:layout_width=
"WRAP_" Content "
android:layout_height=" wrap_content "
android:id=" @+id/button_search "
android:layout_x= "253px"
android:layout_y= "5px"
android:text= "@string/tosearch"
/>
<edittext
Android:layout_width= "Wrap_content"
android:layout_height= "wrap_content"
android:id= "@+id/key
" android:text= "821077962.db"
/>
<textview
android:layout_width= "Fill_parent"
android: layout_height= "370px"
android:id= "@+id/textview_result"
android:layout_x= "0px"
android:layout_y = "60px"
/>
</AbsoluteLayout>
Strings.xml Code:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "Hello" >hello world, activity07!</string>
<string name= "app_name" > File search </string>
<string name= "Tosearch" > Search </string>
<string name= "info" > System sdcard Directory file path:\n</string>
<string name= " Pleaseinput "> Please enter the keyword!</string>
<string name=" NotFound ">SD card has no relevant files!! </string>
<string name= "Patherror" > Read path Error!! </string>
</resources>
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.