標籤:android http os java 使用 io strong for 檔案
本文給大家講解下Android檔案選取器的使用。實際上就是擷取使用者在SD卡中選擇的檔案或檔案夾的路徑,這很像C#中的OpenFileDialog控制項。
此執行個體的實現過程很簡單,這樣可以讓大家快速的熟悉Android檔案選取器,提高開發效率。
網上曾經見到過一個關於檔案選取器的執行個體,很多人都看過,本執行個體是根據它修改而成的,但更容易理解,效率也更高,另外,本執行個體有自己的特點:
1、監聽了使用者按下Back鍵的事件,使其返回上一層目錄。
2、針對不同的檔案類型(檔案vs檔案夾 , 目標檔案vs其他檔案)做了特殊處理。
知識點一、 File 類的使用
檔案選取器的主要功能是:瀏覽檔案\檔案夾、檔案類型等;都是通過Java File類來實現的。
知識點二、調用方法說明
使用了startActivityForResult()發起調用以及onActivityResult()方法接收回調後的資訊。
先貼上如下:
其他的也沒什麼好說了,大家看看代碼注釋吧,很簡單。
FileChooserActivity.java 實現檔案選擇的類 。
Java代碼
- public class CopyOfFileChooserActivity extends Activity {
-
- private String mSdcardRootPath ; //sdcard 根路徑
- private String mLastFilePath ; //當前顯示的路徑
-
- private ArrayList<FileInfo> mFileLists ;
- private FileChooserAdapter mAdatper ;
-
- //配置適配器
- private void setGridViewAdapter(String filePath) {
- updateFileItems(filePath);
- mAdatper = new FileChooserAdapter(this , mFileLists);
- mGridView.setAdapter(mAdatper);
- }
- //根據路徑更新資料,並且通知Adatper資料改變
- 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(); //重新重新整理
- }
- //獲得當前路徑的所有檔案
- 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()) //點擊項為檔案夾, 顯示該檔案夾下所有檔案
- updateFileItems(fileInfo.getFilePath()) ;
- else if(fileInfo.isPPTFile()){ //是ppt檔案 , 則將該路徑通知給調用者
- 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(){
- //判斷當前路徑是不是sdcard路徑 , 如果不是,則返回到上一層。
- if (!mLastFilePath.equals(mSdcardRootPath)) {
- File thisFile = new File(mLastFilePath);
- String parentFilePath = thisFile.getParent();
- updateFileItems(parentFilePath);
- }
- else { //是sdcard路徑 ,直接結束
- setResult(RESULT_CANCELED);
- finish();
- }
- }
- }
此執行個體的介面稍顯簡陋,不過大家可以在此基礎上完善,添加其他功能。本執行個體代碼:http://download.csdn.net/detail/qinjuning/4825392。
Android檔案選取器的執行個體分享