使用ListView显示Android SD卡中的文件列表

父类布局activity_main.xml,子类布局line.xml(一个文件的单独存放)

运行截图:

程序结构:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.asus.gary03_text"> <!--AndroidManifest.xml获取手机存储卡权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

AndroidManifest.xml

package com.example.asus.gary03_text;

import android.support.v7.app.AppCompatActivity;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; 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 MainActivity extends AppCompatActivity { ListView listView;
TextView textView;
//记录当前的父文件夹
File currentParent;
//记录当前目录路径下的所有文件的文件数组
File[] currentFiles;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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(MainActivity.this, "当前路径不可访问或该路径下没有文件",Toast.LENGTH_SHORT).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();
}
}
}

MainActivity

<?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="180dp"
android:layout_height="80dp"
android:text="返回"
android:paddingTop="20dp"
android:layout_gravity="center"/>
</LinearLayout>

activity_main

<?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>

line

一、获取手机存储卡权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

二、界面布局

activity_main.xml布局中

  TextView中显示当前路径的的文本框,ListView列出当前路径下所有文件的ListView ,Button按钮返回上一级目录

link.xml布局中

  ImageView和extView,用于作为列表项的一部分

三、实现程序功能

1、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(MainActivity.this, "当前路径不可访问或该路径下没有文件",Toast.LENGTH_SHORT).show();
}
else{
//获取用户单击的列表项对应的文件夹,设为当前的父文件夹
currentParent = currentFiles[arg2];
//保存当前的父文件夹内的全部文件和文件夹
currentFiles = tmp;
//再次更新ListView
inflateListView(currentFiles);
}
}
});

2、返回按键按钮

  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();
}
}
});
}

3、用List集合存放文件,并且创建一个适配器SimpleAdapter

构造方法:SimpleAdapter(Contextcontext,List>data,intresource,String[]from,int[]to)

context:要使用的上下文环境。

data:是一个List>类型的集合对象,该集合中每个Map对象生成一个列表项。

resource:界面布局文件的ID,对应的布局文件作为列表项的组件。

from:是一个String[]类型的参数,该参数决定提取Map对象中哪些key对应的value来生成列表项。

to:该参数是一个int[]类型的参数,该参数决定填充哪些组件。

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();
}
}

Android_(控件)使用ListView显示Android系统中SD卡的文件列表的更多相关文章

  1. Android_(控件)使用ListView显示Android系统中联系人信息

    使用ListView显示手机中联系人的姓名和电话号码 父类布局activity_main.xml,子类布局line.xml(一个文件的单独存放) 运行截图: (避免泄露信息对部分地方进行了涂鸦O(∩_ ...

  2. Android_(控件)使用ListView显示Android系统SD卡的文件列表_02

    使用ListView显示Android SD卡中的文件列表 父类布局activity_main.xml,子类布局item_filelayout(一个文件的单独存放) 运行截图: 程序结构 <?x ...

  3. Android系统中默认值的意义列表

    转自:http://blog.csdn.net/yabg_zhi_xiang/article/details/51727844 在SettingsProvider中设置系统中默认值,我们可以在fram ...

  4. Android开发之SD卡上文件操作

    1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...

  5. 查看Android系统中硬件信息的文件

    文件目录: 使用Linux命令,进入到/proc目录 进入/proc目录,可以查看内存信息(memoinfo)或CPU信息(cpuinfo),使用cat命令

  6. Android_(控件)使用Gallery浏览手机上SD卡中图片

    运行截图: (发现后面两张照片是自己自拍,大写的尴尬对图片进行涂鸦了!!!) 程序结构: <?xml version="1.0" encoding="utf-8&q ...

  7. Android模拟器使用SD卡

    在Android的应用开发中经常要用到与SD卡有关的调试,本文就是介绍关于在Android模拟器中SD卡的使用 一.      准备工作 在介绍之前首先做好准备工作,即配好android的应用开发环境 ...

  8. Android的WebView控件载入网页显示速度慢的究极解决方案

    Android的WebView控件载入网页显示速度慢的究极解决方案 [转载来源自http://hi.baidu.com/goldchocobo/] 秒(甚至更多)时间才会显示出来.研究了很久,搜遍了国 ...

  9. XE6 FMX之控件绘制与显示

    中午,有个货随手买的2块钱的彩票,尼玛中了540块,这是啥子狗屎气运.稍微吐槽一下,现在开始正规的笔记录入.经常有朋友说为毛我的博客不更新了或者说更新的少了,为啥呢!一来自己懒了,没学习什么新的东西, ...

随机推荐

  1. kafka运维填坑

    转载自:https://www.jianshu.com/p/d2cbaae38014 前提: 只针对Kafka 0.9.0.1版本; 说是运维,其实偏重于问题解决; 大部分解决方案都是google而来 ...

  2. Swagger学习(一、入门)

    简单 入门(效果) SwaggerConfig.class @Configuration //变成配置文件 @EnableSwagger2 //开启swagger2 public class Swag ...

  3. python数字类型之math库使用

    首先我们应当了解什么是math库: math库是python提供的内置数学类函数库,math库不支持复数类型,仅支持整数和浮点数运算.math库一共提供了4个数字常数和44个函数.44个函数共分为4类 ...

  4. VUE生产环境打包build

    1.进入到项目根目录执行 npm run build 此时会自动打包在dist目录下 2.安装服务 npm  install -g serve 3.启动 serve dist 总结: 以上就是生产环境 ...

  5. vue +echarts树状图

    <template> <div :class="className" :id="id" :style="{height:height ...

  6. 利用 Monitor.TryEnter 来规避 .NET 线程死锁的源代码

    在开发多线程的应用程序时,我们会大量用到 lock (...) {} 块.如果 lock 的对象比较多,非常容易发生死锁.死锁的发生很难预料,而且一旦发生在界面线程上,界面就不再刷新响和应用户输入:如 ...

  7. 【转】通过lombok带你读透Builder构建器

    原地址:https://www.jianshu.com/p/0d8fc3df3647?from=timeline&isappinstalled=0 很久之前,我在<effective j ...

  8. 8.2.ZooKeeper应用场景

    7.ZooKeeper应用举例 为了方便大家理解ZooKeeper,在此就给大家举个例子,看看ZooKeeper是如何实现的他的服务的,我以ZooKeeper提供的基本服务分布式锁为例. 7.1 分布 ...

  9. 《Python基础教程》第二章:列表和元组(1)

    列表可以修改,元组则不能 字符串就是一个由字符组成的序列 使用分片操作来访问一定范围内的元素,分片操作的实现需要提供两个索引作为边界,第一个索引的元素是包含在分片内的,而第二个不包含在分片内. 如果分 ...

  10. BBS-media配置

    media配置: 在上传头像的时候会用到media,首先需要在setting中加下面这一句话 MEDIA_ROOT=os.path.join(BASE_DIR,"blog",&qu ...