Android traverses all folders and subdirectories to search for files _android

Source: Internet
Author: User
Tags gettext

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.

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.