Android 搜尋SD卡檔案的開發樣本_Android

來源:互聯網
上載者:User

       我們在進行Android開發時往往需要訪問SD卡的內容,而且因為檔案很多,希望能夠在SD卡中進行搜尋。本文就給出一個Android開發執行個體,示範如何搜尋SD卡裡的檔案。

       執行個體介面

       首先讓那個大家看看程式運行後的介面是什麼樣子的:

       在第一個EditText中設定搜尋的目錄,預設為根目錄"/"。

       第二個EditText為所要搜尋的關鍵字。

       Layout布局 

       下面貼出layout中的布局檔案內容,應該說也是比較簡單的,難度不大。

XML/HTML代碼<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="fill_parent"   android:layout_height="fill_parent"   android:orientation="vertical" >    <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_marginTop="20dp"     android:orientation="horizontal" >           <TextView        android:text="輸入檔案名稱字:"       android:textSize="20dp"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       />     <EditText       android:id="@+id/editText1"       android:textSize="20dp"       android:layout_width="fill_parent"       android:layout_height="wrap_content"       />   </LinearLayout>       <Button      android:id="@+id/button1"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="開始搜尋"     />       <TextView      android:id="@+id/textView1"     android:layout_marginTop="20dp"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     />       </LinearLayout> 

        搜尋功能的代碼實現

        最後就是如何?搜尋的問題。

        我通過java.io.File中定義的File.getName().indexOf(keyword) >= 0 來判斷檔案是否符合搜尋要求。

        具體的實現是通過下面的代碼:

File[] files = new File(file.getPath()).listFiles();            for (File f : files)      {        if (f.getName().indexOf(keyword) >= 0)        {          res += f.getPath() + "\n";        }      } 

       其中,File[] files = new File(file.getPath()).listFiles(); 是用來得到所要求目錄下的所有檔案,再通過 for (File f  :  files)  曆遍所有檔案。

       完整的Main.java 代碼為:

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 when the activity is first created. */   @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 = "不是目錄";        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 = "沒有找到相關檔案";      }             return res;            }  } 

以上就是實現Android 搜尋 SD卡檔案的實現,有需要的朋友可以看下,希望能協助你開發相應的功能。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.