Android 文件選擇器詳解及實(shí)例代碼
本文給大家講解下Android文件選擇器的使用。實(shí)際上就是獲取用戶在SD卡中選擇的文件或文件夾的路徑,這很像C#中的OpenFileDialog控件。
此實(shí)例的實(shí)現(xiàn)過程很簡單,這樣可以讓大家快速的熟悉Android文件選擇器,提高開發(fā)效率。
網(wǎng)上曾經(jīng)見到過一個(gè)關(guān)于文件選擇器的實(shí)例,很多人都看過,本實(shí)例是根據(jù)它修改而成的,但更容易理解,效率也更高,另外,本實(shí)例有自己的特點(diǎn):
1、監(jiān)聽了用戶按下Back鍵的事件,使其返回上一層目錄。
2、針對不同的文件類型(文件vs文件夾 , 目標(biāo)文件vs其他文件)做了特殊處理。
知識點(diǎn)一、 File 類的使用
文件選擇器的主要功能是:瀏覽文件\文件夾、文件類型等;都是通過Java File類來實(shí)現(xiàn)的。
知識點(diǎn)二、調(diào)用方法說明
使用了startActivityForResult()發(fā)起調(diào)用以及onActivityResult()方法接收回調(diào)后的信息。
先貼上效果圖如下:
其他的也沒什么好說了,大家看看代碼注釋吧,很簡單。
FileChooserActivity.java 實(shí)現(xiàn)文件選擇的類 。
Java代碼
public class CopyOfFileChooserActivity extends Activity { private String mSdcardRootPath ; //sdcard 根路徑 private String mLastFilePath ; //當(dāng)前顯示的路徑 private ArrayList<FileInfo> mFileLists ; private FileChooserAdapter mAdatper ; //配置適配器 private void setGridViewAdapter(String filePath) { updateFileItems(filePath); mAdatper = new FileChooserAdapter(this , mFileLists); mGridView.setAdapter(mAdatper); } //根據(jù)路徑更新數(shù)據(jù),并且通知Adatper數(shù)據(jù)改變 private void updateFileItems(String filePath) { mLastFilePath = filePath ; mTvPath.setText(mLastFilePath); if(mFileLists == null) mFileLists = new ArrayList<FileInfo>() ; if(!mFileLists.isEmpty()) mFileLists.clear() ; File[] files = folderScan(filePath); if(files == null) return ; for (int i = 0; i < files.length; i++) { if(files[i].isHidden()) // 不顯示隱藏文件 continue ; String fileAbsolutePath = files[i].getAbsolutePath() ; String fileName = files[i].getName(); boolean isDirectory = false ; if (files[i].isDirectory()){ isDirectory = true ; } FileInfo fileInfo = new FileInfo(fileAbsolutePath , fileName , isDirectory) ; //添加至列表 mFileLists.add(fileInfo); } //When first enter , the object of mAdatper don't initialized if(mAdatper != null) mAdatper.notifyDataSetChanged(); //重新刷新 } //獲得當(dāng)前路徑的所有文件 private File[] folderScan(String path) { File file = new File(path); File[] files = file.listFiles(); return files; } private AdapterView.OnItemClickListener mItemClickListener = new OnItemClickListener() { public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { FileInfo fileInfo = (FileInfo)(((FileChooserAdapter)adapterView.getAdapter()).getItem(position)); if(fileInfo.isDirectory()) //點(diǎn)擊項(xiàng)為文件夾, 顯示該文件夾下所有文件 updateFileItems(fileInfo.getFilePath()) ; else if(fileInfo.isPPTFile()){ //是ppt文件 , 則將該路徑通知給調(diào)用者 Intent intent = new Intent(); intent.putExtra(EXTRA_FILE_CHOOSER, fileInfo.getFilePath()); setResult(RESULT_OK , intent); finish(); } else { //其他文件..... toast(getText(R.string.open_file_error_format)); } } }; public boolean onKeyDown(int keyCode , KeyEvent event){ if(event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_BACK){ backProcess(); return true ; } return super.onKeyDown(keyCode, event); } //返回上一層目錄的操作 public void backProcess(){ //判斷當(dāng)前路徑是不是sdcard路徑 , 如果不是,則返回到上一層。 if (!mLastFilePath.equals(mSdcardRootPath)) { File thisFile = new File(mLastFilePath); String parentFilePath = thisFile.getParent(); updateFileItems(parentFilePath); } else { //是sdcard路徑 ,直接結(jié)束 setResult(RESULT_CANCELED); finish(); } } }
此實(shí)例的界面稍顯簡陋,不過大家可以在此基礎(chǔ)上完善,添加其他功能。本實(shí)例代碼下載地址:
http://download.csdn.net/detail/qinjuning/4825392。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android Studio連接MySql實(shí)現(xiàn)登錄注冊(附源代碼)
登錄注冊是常用的一個(gè)功能,正好今天用android studio 做一個(gè)類似于這樣的登錄軟件,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05android開發(fā)之關(guān)閉所有的activity的方法
本篇文章主要介紹了android開發(fā)之關(guān)閉所有的activity的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12Android實(shí)現(xiàn)整理PackageManager獲取所有安裝程序信息
這篇文章主要介紹了Android實(shí)現(xiàn)整理PackageManager獲取所有安裝程序信息的方法,實(shí)例分析了Android使用PackageManager獲取安裝程序信息的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-01-01Android仿QQ微信實(shí)時(shí)監(jiān)測網(wǎng)絡(luò)狀態(tài)
這篇文章主要為大家詳細(xì)介紹了Android仿QQ微信實(shí)時(shí)監(jiān)測網(wǎng)絡(luò)狀態(tài),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android PopupWindow被輸入法彈上去之后無法恢復(fù)原位的解決辦法
這篇文章主要介紹了Android PopupWindow被輸入法彈上去之后無法恢復(fù)原位的解決辦法,需要的朋友可以參考下2016-12-12Android系列之Intent傳遞對象的幾種實(shí)例方法
Android系列之Intent傳遞對象的幾種實(shí)例方法,需要的朋友可以參考一下2013-05-05