Android_(控件)使用ListView显示Android系统中SD卡的文件列表
使用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卡的文件列表的更多相关文章
- Android_(控件)使用ListView显示Android系统中联系人信息
使用ListView显示手机中联系人的姓名和电话号码 父类布局activity_main.xml,子类布局line.xml(一个文件的单独存放) 运行截图: (避免泄露信息对部分地方进行了涂鸦O(∩_ ...
- Android_(控件)使用ListView显示Android系统SD卡的文件列表_02
使用ListView显示Android SD卡中的文件列表 父类布局activity_main.xml,子类布局item_filelayout(一个文件的单独存放) 运行截图: 程序结构 <?x ...
- Android系统中默认值的意义列表
转自:http://blog.csdn.net/yabg_zhi_xiang/article/details/51727844 在SettingsProvider中设置系统中默认值,我们可以在fram ...
- Android开发之SD卡上文件操作
1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...
- 查看Android系统中硬件信息的文件
文件目录: 使用Linux命令,进入到/proc目录 进入/proc目录,可以查看内存信息(memoinfo)或CPU信息(cpuinfo),使用cat命令
- Android_(控件)使用Gallery浏览手机上SD卡中图片
运行截图: (发现后面两张照片是自己自拍,大写的尴尬对图片进行涂鸦了!!!) 程序结构: <?xml version="1.0" encoding="utf-8&q ...
- Android模拟器使用SD卡
在Android的应用开发中经常要用到与SD卡有关的调试,本文就是介绍关于在Android模拟器中SD卡的使用 一. 准备工作 在介绍之前首先做好准备工作,即配好android的应用开发环境 ...
- Android的WebView控件载入网页显示速度慢的究极解决方案
Android的WebView控件载入网页显示速度慢的究极解决方案 [转载来源自http://hi.baidu.com/goldchocobo/] 秒(甚至更多)时间才会显示出来.研究了很久,搜遍了国 ...
- XE6 FMX之控件绘制与显示
中午,有个货随手买的2块钱的彩票,尼玛中了540块,这是啥子狗屎气运.稍微吐槽一下,现在开始正规的笔记录入.经常有朋友说为毛我的博客不更新了或者说更新的少了,为啥呢!一来自己懒了,没学习什么新的东西, ...
随机推荐
- asp.net 9 ViewState
VIEWSTATE aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=&quo ...
- pycharm2017.3.3永久激活(转载)
pycharm是很强大的开发工具,但是每次注册着实让人头疼.网络上很多注册码.注册服务器等等.但都只是一年或者不能用:为次有如下解决方案.亲测有效!!! 如果想让pycharm永久被激活,比如截止日到 ...
- JS基础_运算符的优先级
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- WebStorm 使用技巧
常用快捷键 代码编辑 ctrl + d:复制行 ctrl + y:删除行 ctrl + x:剪切行 ctrl + shift + ↑: 行移动 ctrl + shift + enter: 换行 ctr ...
- 封装好的Ajax
/* 注意: 1.检查提交方式类型:get/post是否和后台一致(该类型接口文档会标注,若一致仍然报错,请与后台再次确认) 2.检查接口url是否写错 3.检查接口是否需要传数据到后台,若需上传,便 ...
- React学习——子组件给父组件传值
//子组件 var Child = React.createClass({ render: function(){ return ( <div> 请输入邮箱:<input onCha ...
- 正着打星星(js)
//让用户输入行数,使用for循环嵌套打出正着的星星来,行数等于用户输入的数字 //例如:用户输入6 // * // *** // ***** // ******* // ********* // * ...
- SpringBoot面试题
详见:https://www.cnblogs.com/3xmq/p/springboot.html https://blog.csdn.net/yuzongtao/article/details/84 ...
- 【转】__cplusplus的含义
有点代码中会看到以下形式的代码: #ifdef __cplusplus extern "C" {#endif #ifdef __cplusplus}#endif 这些代码是什么意思 ...
- 内核模式构造-Semaphore构造(WaitLock)
internal sealed class SimpleWaitLock : IDisposable { //(信号量)允许多个线程并发访问一个资源 //如果所有线程以只读方式访问资源则是安全的 pr ...