Android 搜索SD卡文件的開(kāi)發(fā)示例
我們?cè)谶M(jìn)行Android開(kāi)發(fā)時(shí)往往需要訪問(wèn)SD卡的內(nèi)容,而且因?yàn)槲募芏?,希望能夠在SD卡中進(jìn)行搜索。本文就給出一個(gè)Android開(kāi)發(fā)實(shí)例,演示如何搜索SD卡里的文件。
實(shí)例界面
首先讓那個(gè)大家看看程序運(yùn)行后的界面是什么樣子的:
在第一個(gè)EditText中設(shè)置搜索的目錄,默認(rèn)為根目錄"/"。
第二個(gè)EditText為所要搜索的關(guān)鍵字。
Layout布局
下面貼出layout中的布局文件內(nèi)容,應(yīng)該說(shuō)也是比較簡(jiǎn)單的,難度不大。
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="開(kāi)始搜索" /> <TextView android:id="@+id/textView1" android:layout_marginTop="20dp" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
搜索功能的代碼實(shí)現(xiàn)
最后就是如何實(shí)現(xiàn)搜索的問(wèn)題。
我通過(guò)java.io.File中定義的File.getName().indexOf(keyword) >= 0 來(lái)判斷文件是否符合搜索要求。
具體的實(shí)現(xiàn)是通過(guò)下面的代碼:
File[] files = new File(file.getPath()).listFiles(); for (File f : files) { if (f.getName().indexOf(keyword) >= 0) { res += f.getPath() + "\n"; } }
其中,F(xiàn)ile[] files = new File(file.getPath()).listFiles(); 是用來(lái)得到所要求目錄下的所有文件,再通過(guò) 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 = "沒(méi)有找到相關(guān)文件"; } return res; } }
以上就是實(shí)現(xiàn)Android 搜索 SD卡文件的實(shí)現(xiàn),有需要的朋友可以看下,希望能幫助你開(kāi)發(fā)相應(yīng)的功能。
- Android SD卡上文件操作及記錄日志操作實(shí)例分析
- 將文件放到Android模擬器的SD卡中的兩種解決方法
- Android獲取assets文件夾中的數(shù)據(jù)并寫(xiě)入SD卡示例
- android通過(guò)配置文件設(shè)置應(yīng)用安裝到SD卡上的方法
- 基于Android掃描sd卡與系統(tǒng)文件的介紹
- Android編程之在SD卡上進(jìn)行文件讀寫(xiě)操作實(shí)例詳解
- 最簡(jiǎn)單的SD卡文件遍歷Android程序
- Android實(shí)現(xiàn)讀取SD卡下所有TXT文件名并用listView顯示出來(lái)的方法
- android 手機(jī)SD卡讀寫(xiě)操作(以txt文本為例)實(shí)現(xiàn)步驟
- android讀寫(xiě)sd卡操作寫(xiě)入數(shù)據(jù)讀取數(shù)據(jù)示例
- Android開(kāi)發(fā)之SD卡文件操作分析
相關(guān)文章
Android Imageloader的配置的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android Imageloader的配置的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-07-07Android仿iOS實(shí)現(xiàn)側(cè)滑返回功能(類(lèi)似微信)
這篇文章主要為大家詳細(xì)介紹了Android仿iOS實(shí)現(xiàn)側(cè)滑返回功能,類(lèi)似微信功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android開(kāi)發(fā)之日歷CalendarView用法示例
這篇文章主要介紹了Android開(kāi)發(fā)之日歷CalendarView用法,簡(jiǎn)單分析了日歷CalendarView組件的功能、屬性設(shè)置方法、界面布局、事件監(jiān)聽(tīng)等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03Android SDK Manager無(wú)法更新問(wèn)題解決辦法
這篇文章主要介紹了Android SDK Manager無(wú)法更新問(wèn)題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04Android使用HttpURLConnection實(shí)現(xiàn)網(wǎng)絡(luò)訪問(wèn)流程
早些時(shí)候其實(shí)我們都習(xí)慣性使用HttpClient,但是后來(lái)Android6.0之后不再支持HttpClient,需要添加Apache的jar才行,所以,就有很多開(kāi)發(fā)者放棄使用HttpClient了,HttpURLConnection畢竟是標(biāo)準(zhǔn)Java接口(java.net) ,適配性還是很強(qiáng)的2022-12-12Android實(shí)現(xiàn)畫(huà)板功能(二)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)畫(huà)板功能的第二篇,使用imageView,bitmap方式實(shí)現(xiàn)畫(huà)板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05android監(jiān)聽(tīng)View加載完成的示例講解
今天小編就為大家分享一篇android監(jiān)聽(tīng)View加載完成的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09詳談自定義View之GridView單選 金額選擇Layout-ChooseMoneyLayout
下面小編就為大家?guī)?lái)一篇詳談自定義View之GridView單選 金額選擇Layout-ChooseMoneyLayout。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05Android 字符串中某個(gè)字段可點(diǎn)擊和設(shè)置顏色的方法
在android開(kāi)發(fā)中,我們時(shí)常會(huì)遇到對(duì)字符串中某些固定的字段實(shí)現(xiàn)可點(diǎn)擊和顏色的設(shè)置,現(xiàn)粘貼處我在開(kāi)發(fā)中如何設(shè)置這些屬性的2017-07-07