本文给大家讲解下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 ;    
-     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);  
-     }  
-       
-     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);  
-         }  
-           
-         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()){    
-                 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(){  
-           
-         if (!mLastFilePath.equals(mSdcardRootPath)) {    
-             File thisFile = new File(mLastFilePath);  
-             String parentFilePath = thisFile.getParent();  
-             updateFileItems(parentFilePath);  
-         }   
-         else {     
-             setResult(RESULT_CANCELED);  
-             finish();  
-         }  
-     }  
- }  
       此实例的界面稍显简陋,不过大家可以在此基础上完善,添加其他功能。本实例代码下载地址:http://download.csdn.net/detail/qinjuning/4825392。