Android檔案選取器的執行個體分享,android選取器執行個體

來源:互聯網
上載者:User

Android檔案選取器的執行個體分享,android選取器執行個體

     本文給大家講解下Android檔案選取器的使用。實際上就是擷取使用者在SD卡中選擇的檔案或檔案夾的路徑,這很像C#中的OpenFileDialog控制項。

       此執行個體的實現過程很簡單,這樣可以讓大家快速的熟悉Android檔案選取器,提高開發效率。

       網上曾經見到過一個關於檔案選取器的執行個體,很多人都看過,本執行個體是根據它修改而成的,但更容易理解,效率也更高,另外,本執行個體有自己的特點:

       1、監聽了使用者按下Back鍵的事件,使其返回上一層目錄。

       2、針對不同的檔案類型(檔案vs檔案夾 , 目標檔案vs其他檔案)做了特殊處理。

       知識點一、 File 類的使用

       檔案選取器的主要功能是:瀏覽檔案\檔案夾、檔案類型等;都是通過Java File類來實現的。

       知識點二、調用方法說明   

       使用了startActivityForResult()發起調用以及onActivityResult()方法接收回調後的資訊。

       先貼上如下:

       其他的也沒什麼好說了,大家看看代碼注釋吧,很簡單。

       FileChooserActivity.java 實現檔案選擇的類 。

Java代碼
  1. public class CopyOfFileChooserActivity extends Activity {  
  2.       
  3.     private String mSdcardRootPath ;  //sdcard 根路徑  
  4.     private String mLastFilePath ;    //當前顯示的路徑  
  5.       
  6.     private ArrayList<FileInfo> mFileLists  ;  
  7.     private FileChooserAdapter mAdatper ;  
  8.       
  9.     //配置適配器  
  10.     private void setGridViewAdapter(String filePath) {  
  11.         updateFileItems(filePath);  
  12.         mAdatper = new FileChooserAdapter(this , mFileLists);  
  13.         mGridView.setAdapter(mAdatper);  
  14.     }  
  15.     //根據路徑更新資料,並且通知Adatper資料改變  
  16.     private void updateFileItems(String filePath) {  
  17.         mLastFilePath = filePath ;  
  18.         mTvPath.setText(mLastFilePath);  
  19.           
  20.         if(mFileLists == null)  
  21.             mFileLists = new ArrayList<FileInfo>() ;  
  22.         if(!mFileLists.isEmpty())  
  23.             mFileLists.clear() ;  
  24.           
  25.         File[] files = folderScan(filePath);  
  26.         if(files == null)   
  27.             return ;  
  28.         for (int i = 0; i < files.length; i++) {  
  29.             if(files[i].isHidden())  // 不顯示隱藏檔案  
  30.                 continue ;  
  31.               
  32.             String fileAbsolutePath = files[i].getAbsolutePath() ;  
  33.             String fileName = files[i].getName();  
  34.             boolean isDirectory = false ;  
  35.             if (files[i].isDirectory()){  
  36.                 isDirectory = true ;  
  37.             }  
  38.             FileInfo fileInfo = new FileInfo(fileAbsolutePath , fileName , isDirectory) ;  
  39.             //添加至列表  
  40.             mFileLists.add(fileInfo);  
  41.         }  
  42.         //When first enter , the object of mAdatper don't initialized  
  43.         if(mAdatper != null)  
  44.             mAdatper.notifyDataSetChanged();  //重新重新整理  
  45.     }  
  46.     //獲得當前路徑的所有檔案  
  47.     private File[] folderScan(String path) {  
  48.         File file = new File(path);  
  49.         File[] files = file.listFiles();  
  50.         return files;  
  51.     }  
  52.     private AdapterView.OnItemClickListener mItemClickListener = new OnItemClickListener() {  
  53.         public void onItemClick(AdapterView<?> adapterView, View view, int position,  
  54.                 long id) {  
  55.             FileInfo fileInfo = (FileInfo)(((FileChooserAdapter)adapterView.getAdapter()).getItem(position));  
  56.             if(fileInfo.isDirectory())   //點擊項為檔案夾, 顯示該檔案夾下所有檔案  
  57.                 updateFileItems(fileInfo.getFilePath()) ;  
  58.             else if(fileInfo.isPPTFile()){  //是ppt檔案 , 則將該路徑通知給調用者  
  59.                 Intent intent = new Intent();  
  60.                 intent.putExtra(EXTRA_FILE_CHOOSER, fileInfo.getFilePath());  
  61.                 setResult(RESULT_OK , intent);  
  62.                 finish();  
  63.             }  
  64.             else {   //其他檔案.....  
  65.                 toast(getText(R.string.open_file_error_format));  
  66.             }  
  67.         }  
  68.     };  
  69.     public boolean onKeyDown(int keyCode , KeyEvent event){  
  70.         if(event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode()  
  71.             == KeyEvent.KEYCODE_BACK){  
  72.             backProcess();     
  73.             return true ;  
  74.         }  
  75.         return super.onKeyDown(keyCode, event);  
  76.     }  
  77.     //返回上一層目錄的操作  
  78.     public void backProcess(){  
  79.         //判斷當前路徑是不是sdcard路徑 , 如果不是,則返回到上一層。  
  80.         if (!mLastFilePath.equals(mSdcardRootPath)) {    
  81.             File thisFile = new File(mLastFilePath);  
  82.             String parentFilePath = thisFile.getParent();  
  83.             updateFileItems(parentFilePath);  
  84.         }   
  85.         else {   //是sdcard路徑 ,直接結束  
  86.             setResult(RESULT_CANCELED);  
  87.             finish();  
  88.         }  
  89.     }  
  90. }  

       此執行個體的介面稍顯簡陋,不過大家可以在此基礎上完善,添加其他功能。本執行個體代碼:http://download.csdn.net/detail/qinjuning/4825392。


Android編程 開啟本地檔案 檔案選取器

布局檔案
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/b01" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv01" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>代碼
import java.io.FileNotFoundException; import android.app.Activity; import android.content.ContentResolver; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class Lesson_01_Pic extends Activity { /** Called when the activity is first created. */ @Override public voi......餘下全文>>
 
檔案夾選取器怎實現

Android中檔案選取器的實現

今天給大家分享下檔案選取器的作用 , 具體就是擷取使用者在在SD卡選中的檔案/檔案夾路徑 ,類似於C#中
OpenFileDialog控制項(對C#的一站式開發還是念念不忘)。功能實現起來比較簡單,主要是協助大家節省開發時間。

網上流傳較廣的一個成品如下 <[Android執行個體] 檔案選取器>, 本文也是根據上面的成品修改而成,使其更易
理解,效率更高。 除此之外,主要特色有:
1、我們監聽了使用者按下Back鍵的事件,使其返回上一層目錄;
2、針對不同的檔案類型(檔案vs檔案夾 , 目標檔案vs其他檔案)做了特殊處理。

知識點一、 File 類的使用
檔案選取器的主要功能是:瀏覽檔案\檔案夾、檔案類型等;都是通過Java File類來實現的。
關於File類的使用,請參考如下部落格:
<<來自java文檔 File類>>
<<Java File類>>

知識點二、調用方法說明
使用了startActivityForResult()發起調用以及onActivityResult()方法接受回調後的資訊。

其他的也沒什麼好說了,大家看看代碼注釋吧~~ so easy - - 。

FileChooserActivity.java 實現檔案選擇的類 。

[java] view plaincopyprint?
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......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.