Android中文件选择器的实现
来源:互联网 发布:淘宝网男士休闲直筒裤 编辑:程序博客网 时间:2024/06/16 17:27
转载请注明出处:http://blog.csdn.net/qinjuning
今天给大家分享下文件选择器的作用 , 具体就是获取用户在在SD卡选中的文件/文件夹路径 ,类似于C#中
OpenFileDialog控件(对C#的一站式开发还是念念不忘)。功能实现起来比较简单,主要是帮助大家节省开发时间。
网上流传较广的一个成品如下 <[Android实例] 文件选择器>, 本文也是根据上面的成品修改而成,使其更易
理解,效率更高。 除此之外,主要特色有:
1、我们监听了用户按下Back键的事件,使其返回上一层目录;
2、针对不同的文件类型(文件vs文件夹 , 目标文件vs其他文件)做了特殊处理。
知识点一、 File 类的使用
文件选择器的主要功能是:浏览文件\文件夹、文件类型等;都是通过Java File类来实现的。
关于File类的使用,请参考如下博客:
<<来自java文档 File类>>
<<Java File类>>
知识点二、调用方法说明
使用了startActivityForResult()发起调用以及onActivityResult()方法接受回调后的信息。
截图如下:
其他的也没什么好说了,大家看看代码注释吧~~ so easy - - 。
FileChooserActivity.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 initializedif(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/4908609 (源代码下载)
- Android中文件选择器的实现
- Android中文件选择器的实现
- Android中文件选择器的实现
- java中文件选择器JFileChooser的用法
- java中文件选择器JFileChooser的用法
- java中文件选择器JFileChooser的用法
- Java Swing中文件选择器的使用
- android中文件加密和解密的实现
- android中文件加密和解密的实现
- Android 中文件的操作
- Java的中文件选择器JFileChooser中的用法
- jsp中文件下载的实现
- jsp中文件下载的实现
- struts2中文件上传的实现
- jsp中文件下载的实现
- struts2中文件上传的实现
- jsp中文件下载的实现
- Netty中文件上传的实现
- ORACLE 的decode函数介绍
- VMware workstation不完全卸载后无法安装的解决方法
- Mysql 的字符串连接
- Qt 右击菜单的示例
- mysql 替换函数replace()实现mysql 替换字符串
- Android中文件选择器的实现
- C++标准转换运算符const_cast
- C语言中time函数
- android Activity的生命周期于四种加载状态
- 【Android Training - 04】保存数据 [ Lesson 0 - 章节概览 ]
- Flex TLF 分页代码
- struts学习<html:link>标签传递参数
- VS2010连接mysql数据库
- ubuntu获得root权限