`
bcyy
  • 浏览: 1826362 次
文章分类
社区版块
存档分类
最新评论

Android的数据存储和IO-SD卡文件浏览器

 
阅读更多

Android的数据存储和IO-SD卡文件浏览器

创建项目:SDFileExplorer

运行项目结果:

布局文件:main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	>
<!-- 显示当前路径的的文本框 -->
<TextView  
	android:id="@+id/path"
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content" 
	android:layout_gravity="center_horizontal" 
	/>
<!-- 列出当前路径下所有文件的ListView -->
<ListView  
	android:id="@+id/list"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content" 
	android:divider="#000"
	/>
<!-- 返回上一级目录的按钮 -->
<Button android:id="@+id/parent"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content" 
	android:background="@drawable/home"
	android:paddingTop="20dp"
	android:layout_gravity="center"/>
</LinearLayout>


列表布局文件: line.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	>	
<!-- 定义一个ImageView,用于作为列表项的一部分。 -->
<ImageView android:id="@+id/icon"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content" 
	android:paddingLeft="10dp"
	/>
<!-- 定义一个TextView,用于作为列表项的一部分。 -->
<TextView android:id="@+id/file_name"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content"
	android:textSize="16dp"
	android:gravity="center_vertical"
	android:paddingLeft="10dp"
	android:paddingTop="10dp"
	android:paddingBottom="10dp"	
	/>	
</LinearLayout>


Activity文件:SDFileExplorer.java

package wwj.sdfileexplorer;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class SDFileExplorer extends Activity {
	
	ListView listView;
	TextView textView;
	//记录当前的父文件夹
	File currentParent;
	//记录当前目录路径下的所有文件的文件数组
	File[] currentFiles;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //获取列出全部文件的ListView
        listView = (ListView)findViewById(R.id.list);
        textView = (TextView)findViewById(R.id.path);
        //获取系统的SD卡的目录
        File root = new File("/mnt/sdcard/");
        //如果SD卡存在
        if(root.exists()){
        	currentParent = root;
        	currentFiles = root.listFiles();
        	//使用当前目录下的全部文件、文件夹来填充ListView
        	inflateListView(currentFiles);
        }
        //为ListView的列表项的单击事件绑定监听器
        listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				//用户单击了文件,直接返回,不做任何处理
				if(currentFiles[arg2].isFile())
					return;
				//获取用户单击的文件夹下的所有文件
				File[] tmp = currentFiles[arg2].listFiles();
				if(tmp == null || tmp.length == 0){
					Toast.makeText(SDFileExplorer.this, "当前路径不可访问或该路径下没有文件", 20000).show();
				}
				else{
					//获取用户单击的列表项对应的文件夹,设为当前的父文件夹
					currentParent = currentFiles[arg2];
					//保存当前的父文件夹内的全部文件和文件夹
					currentFiles = tmp;
					//再次更新ListView
					inflateListView(currentFiles);
				}
			}
		});
        //获取上一级目录的按钮
        Button parent = (Button)findViewById(R.id.parent);
        parent.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try{
					if(!currentParent.getCanonicalPath().equals("/mnt/sdcard")){
						//获取上级目录
						currentParent = currentParent.getParentFile();
						//列出当前目录下所有文件
						currentFiles = currentParent.listFiles();
						//再次更新ListView
						inflateListView(currentFiles);
					}
				}catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		});
    }
    private void inflateListView(File[] files){
    	//创建一个List集合,List集合的元素是Map
    	List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
    	for(int i = 0; i < files.length; i++){
    		Map<String, Object> listItem = new HashMap<String, Object>();
    		//如果当前File是文件夹,使用floder图标;否则使用file图标
    		if(files[i].isDirectory()){
    			listItem.put("icon", R.drawable.folder);
    		}
    		else{
    			listItem.put("icon", R.drawable.file);
    		}
    		listItem.put("fileName", files[i].getName());
    		//添加List项
    		listItems.add(listItem);
    	}
    	//创建一个SimpleAdapter
    	SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.line, 
    			new String[]{"icon","fileName"}, new int[]{R.id.icon, R.id.file_name});
    	//为ListView设置Adapter
    	listView.setAdapter(simpleAdapter);
    	try{
    		textView.setText("当前路径为: " + currentParent.getCanonicalPath());
    	}catch (Exception e) {
			// TODO: handle exception
    		e.printStackTrace();
		}
    }
}


分享到:
评论

相关推荐

    commons-io-2.8.0-API文档-中英对照版.zip

    赠送jar包:commons-io-2.8.0.jar; 赠送原API文档:commons-io-2.8.0-javadoc.jar; 赠送源代码:commons-io-2.8.0-sources.jar; 赠送Maven依赖信息文件:commons-io-2.8.0.pom; 包含翻译后的API文档:commons-io...

    Android代码-card.io-Android-SDK

    card.io SDK for Android card.io provides fast, easy credit card scanning in mobile apps. Stay up to date Please be sure to keep your app up to date with the latest version of the SDK. All releases ...

    commons-io-2.11.0-API文档-中文版.zip

    赠送jar包:commons-io-2.11.0.jar; 赠送原API文档:commons-io-2.11.0-javadoc.jar; 赠送源代码:commons-io-2.11.0-sources.jar; 赠送Maven依赖信息文件:commons-io-2.11.0.pom; 包含翻译后的API文档:...

    commons-io-2.5-API文档-中文版.zip

    赠送jar包:commons-io-2.5.jar; 赠送原API文档:commons-io-2.5-javadoc.jar; 赠送源代码:commons-io-2.5-sources.jar; 赠送Maven依赖信息文件:commons-io-2.5.pom; 包含翻译后的API文档:commons-io-2.5-...

    commons-io-1.3.2-API文档-中文版.zip

    赠送jar包:commons-io-1.3.2.jar; 赠送原API文档:commons-io-1.3.2-javadoc.jar; 赠送源代码:commons-io-1.3.2-sources.jar; 赠送Maven依赖信息文件:commons-io-1.3.2.pom; 包含翻译后的API文档:commons-io...

    commons-io-2.7-API文档-中文版.zip

    赠送jar包:commons-io-2.7.jar; 赠送原API文档:commons-io-2.7-javadoc.jar; 赠送源代码:commons-io-2.7-sources.jar; 赠送Maven依赖信息文件:commons-io-2.7.pom; 包含翻译后的API文档:commons-io-2.7-...

    IO-Link IOL-Interface-Spec_10002_V113_Jun19.pdf

    7、Device实现的功能 包括:参数管理(Parameter Manager )、过程数据交换(Process Data Exchange)、数据存储(Data Storage 即DS )、IODD、事件(event)、设备诊断(device diagnosis); 8、Master实现的功能...

    commons-io-1.4-API文档-中文版.zip

    赠送jar包:commons-io-1.4.jar; 赠送原API文档:commons-io-1.4-javadoc.jar; 赠送源代码:commons-io-1.4-sources.jar; 赠送Maven依赖信息文件:commons-io-1.4.pom; 包含翻译后的API文档:commons-io-1.4-...

    commons-io-2.4-API文档-中文版.zip

    赠送jar包:commons-io-2.4.jar; 赠送原API文档:commons-io-2.4-javadoc.jar; 赠送源代码:commons-io-2.4-sources.jar; 赠送Maven依赖信息文件:commons-io-2.4.pom; 包含翻译后的API文档:commons-io-2.4-...

    commons-io-2.8.0-API文档-中文版.zip

    赠送jar包:commons-io-2.8.0.jar; 赠送原API文档:commons-io-2.8.0-javadoc.jar; 赠送源代码:commons-io-2.8.0-sources.jar; 赠送Maven依赖信息文件:commons-io-2.8.0.pom; 包含翻译后的API文档:commons-io...

    commons-io-2.2-API文档-中英对照版.zip

    使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。

    common-io-3.1.1-API文档-中英对照版.zip

    赠送jar包:common-io-3.1.1.jar; 赠送原API文档:common-io-3.1.1-javadoc.jar; 赠送源代码:common-io-3.1.1-sources.jar; 赠送Maven依赖信息文件:common-io-3.1.1.pom; 包含翻译后的API文档:common-io-...

    socket.io,socket.io-client下载

    socket.io,socket.io-client 用于java或android连接nodejs websocket

    io-7.1.2-API文档-中英对照版.zip

    赠送jar包:io-7.1.2.jar; 赠送原API文档:io-7.1.2-javadoc.jar; 赠送源代码:io-7.1.2-sources.jar; 赠送Maven依赖信息文件:io-7.1.2.pom; 包含翻译后的API文档:io-7.1.2-javadoc-API文档-中文(简体)-英语-...

    jetty-io-9.4.43.v20210629-API文档-中英对照版.zip

    赠送jar包:jetty-io-9.4.43.v20210629.jar; 赠送原API文档:jetty-io-9.4.43.v20210629-javadoc.jar; 赠送源代码:jetty-io-9.4.43.v20210629-sources.jar; 赠送Maven依赖信息文件:jetty-io-9.4.43.v20210629....

    jetty-io-9.4.43.v20210629-API文档-中文版.zip

    赠送jar包:jetty-io-9.4.43.v20210629.jar; 赠送原API文档:jetty-io-9.4.43.v20210629-javadoc.jar; 赠送源代码:jetty-io-9.4.43.v20210629-sources.jar; 赠送Maven依赖信息文件:jetty-io-9.4.43.v20210629....

    实验10-Android数据存储和IO.doc

    实验报告封面 课程名称: Android平台开发与应用 课程代码: SM3004 任课老师: 梁郁君 实验指导老师: 梁郁君 实验报告名称:实验10 Android数据存储与IO 学生姓名: 学号: 教学班: 递交日期: 签收人: 我申明,...

    commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar程序文件

    commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar 案例上传: http://hi.baidu.com/lichao77821/blog

    containerd.io-1.2.6-3.3.el7.x86_64.rpm

    containerd.io-1.2.6-3.3.el7.x86_64.rpm离线安装包。

Global site tag (gtag.js) - Google Analytics