亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android 文件選擇器詳解及實(shí)例代碼

 更新時(shí)間:2016年10月23日 08:50:18   投稿:lqh  
這篇文章主要介紹了Android 文件選擇器詳解的相關(guān)資料,并附實(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)文章

最新評論