Android檔案選取器的執行個體分享,android選取器執行個體
本文給大家講解下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編程 開啟本地檔案 檔案選取器
布局檔案
<?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......餘下全文>>