We often need access to the contents of the SD card in the development of Android, and because there are so many files, we want to search the SD card. This article gives an example of an Android development that demonstrates how to search for files in an SD card.
Instance interface
First let's see what the interface looks like when the program is running:
Set the search directory in the first edittext, which defaults to the root directory "/".
The second edittext is the keyword to search for.
Layout layout
The following post the layout of the layout in the contents of the file, it should be said is relatively simple, not very difficult.
xml/html Code <?xml version= "1.0" encoding= "Utf-8"?> <linearlayout "xmlns:android=" Apk/res/android "android:layout_width=" fill_parent "android:layout_height=" fill_parent "android:orientation=" vert ical "> <edittext android:id=" @+id/edittext2 "android:layout_width=" Fill_parent "android:layout _height= "Wrap_content" android:text= "/"/> <linearlayout xmlns:android= "http://schemas.android.com /apk/res/android "android:layout_width=" fill_parent "android:layout_height=" Wrap_content "Android:layout_ma" rgintop= "20DP" android:orientation= "horizontal" > <textview android:text= "input file name:" A
Ndroid:textsize= "20DP" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"/> <edittext android:id= "@+id/edittext1" android:textsize= "20DP" android:layout_width= "Fill_par" Ent "Android:layout_height= "Wrap_content"/> </LinearLayout> <button android:id= "@+id/button1" Android: Layout_width= "Fill_parent" android:layout_height= "wrap_content" android:text= "Start Search"/> <TextV Iew android:id= "@+id/textview1" android:layout_margintop= "20DP" android:layout_width= "Fill_parent" an
droid:layout_height= "Fill_parent"/> </LinearLayout>
Code implementation of the search function
The last is how to achieve the search problem.
I use the File.getname (). INDEXOF (keyword) >= defined in Java.io.File to determine whether the file meets the search requirements.
The specific implementation is through the following code:
file[] files = new File (File.getpath ()). Listfiles ();
for (File f:files)
{
if (F.getname (). INDEXOF (keyword) >= 0)
{
res = F.getpath () + "\ n";
}
}
where file[] files = new File (File.getpath ()). Listfiles (); is used to get all the files in the requested directory, and then through the for (file f:files) calendar all the files.
The complete Main.java code is:
Package net.javablog.mobile;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.Button;
Import Android.widget.TextView;
Import Android.widget.EditText;
Import Android.view.View;
Import Java.io.File;
public class Main extends activity {private TextView textView1;
Private EditText EditText1;
Private EditText editText2;
Private Button button1; /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
TextView1 = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
EditText1 = (edittext) Findviewbyid (R.ID.EDITTEXT1);
EditText2 = (edittext) Findviewbyid (R.ID.EDITTEXT2);
Button1 = (Button) Findviewbyid (R.id.button1);
Button1.setonclicklistener (New Button.onclicklistener () {@Override public void OnClick (view view) {
String keyword = edittext1.gettext (). toString ();
File root = new file (Edittext2.gettext (). toString ());
if (Keyword.equals ("")) {String res = "";
file[] files = root.listfiles ();
for (File f:files) {res = F.getpath () + \ n;
} textview1.settext (res);
Return
else {Textview1.settext (findfile (root, keyword));
} return;
}
});
private string FindFile (file file, string keyword) {string res = "";
if (!file.isdirectory ()) {res = "not a directory";
return res;
} file[] Files = new File (File.getpath ()). Listfiles (); for (File f:files) {if (F.getname (). INDEXOF (keyword) >= 0) {res = F.getpath () + "\
n ";
} if (Res.equals ("")) {res = "No related files found";
return res;
}
}
The above is to achieve the Android search &NBSP;SD card file implementation, the need for friends can see, hope to help you develop the corresponding functions.