使用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. c# winfrom 界面设计

    1.在用DotnetBar的RibbonControl时,界面最大化时,会把电脑桌面的任务栏遮盖住: 解决办法:在load事件中写入: , Screen.PrimaryScreen.WorkingAr ...

  2. unity 3D循环滚动效果

    https://blog.csdn.net/qinyuanpei/article/details/52765356 https://blog.csdn.net/chongzi_daima/articl ...

  3. 解决EntityFramework与System.ComponentModel.DataAnnotations命名冲突

    比如,定义entity时指定一个外键, [ForeignKey("CustomerID")] public Customer Customer { get; set; } 编译时报 ...

  4. 谁是嫌疑犯问题Python枚举法

    原文:https://blog.csdn.net/yunzifengqing/article/details/81941592 问题描述:有6名犯罪嫌疑人A.B.C.D.E.F,已知如下事实: A.B ...

  5. 配置lombok到eclipse上去

    使用maven导入lombok.jar包,可以帮助我们省略掉getter/setting方法. 1.pom.xml 添加依赖: <dependency> <groupId>or ...

  6. Centos7:ActiveMQ安装,配置及使用

    解压缩ActiveMQ 的压缩包 使用 命令在bin目录下 ./activemq stat//开启 ./activemq stop//关闭 ./activemq status//状态 进入管理后台 U ...

  7. Docker 容器数据卷(Data Volume)与数据管理

    卷(Volume)是容器中的一个数据挂载点,卷可以绕过联合文件系统,从而为Docker 提供持久数据,所提供的数据还可以在宿主机-容器或多个容器之间共享.通过卷,我们可以可以使修改数据直接生效,而不必 ...

  8. vue项目使用qrcodejs2生成二维码

    最近写项目遇到一个需求,根据后台给的地址生成二维码,在网上找了下,qrcodejs2使用还是比较多,试了下也能实现需求,就整理下使用方法,方便以后使用   1. 安装包 cnpm i qrcodejs ...

  9. ASP.NET实现验证码图片

    新建一个checkcode.aspx文件,页面中不用写任何东西,在代码中,Page_Load中写入如下代码: string chkCode = string.Empty;        int ix, ...

  10. celery:强大的定时任务模块

    什么是celery 还是一个老生常谈的话题,假设用户注册,首先注册信息入库,然后要调用验证码服务接口,然后根据手机号发送验证码,最后再返回响应给浏览器.但显然调用接口.发送验证码之后成功再给浏览器响应 ...