Android traverse all folders and subdirectories to search for files

Source: Internet
Author: User
Tags gettext

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 classShuosouwenjianactivity extends Activity implements Onclicklistener {Privatefile file; PrivateString Path; PrivateString Info; PrivateString key;//Key Words    PrivateTextView result;//Show Results    PrivateEditText et;//Edit View    PrivateButton search_btn;//button View@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);              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 =NewFile ("/sdcard/"); Info= GetString (R.string. info); Search_btn.setonclicklistener ( This); } @Override Public voidOnClick (View v) {//TODO auto-generated Method StubPath =""; Result.settext (""); Key=Et.gettext (). toString ();      Browserfile (file); }            Public voidbrowserfile (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 voidSearch (File fileold) {Try{file[] files=Fileold.listfiles (); if(files.length>0)  {      for(intj=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> <stringName="Hello">hello World, activity07!</string> <stringName="app_name"> File Search </string> <stringName="Tosearch"> Search </string> <stringName="Info"> System sdcard Directory file path: \n</string> <stringName="Pleaseinput"> Please enter the keyword!</string> <stringName="NotFound"&GT;SD card does not have the relevant files!! </string> <stringName="Patherror"> Error reading Path!! </string> </resources>

Android traverse all folders and subdirectories to search for files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.