模拟一个sd卡所有文件浏览器的功能:
1、通过File的listFile()方法获取指定目录下的全部文件和文件夹
2、定义String currentParent 变量,为当前的父文件夹、File currentFiles()为当前的所有文件
3、将获取到的所有文件放在ListView中。
4、如果文件时个文件夹,那么显示文件夹的图标,否则显示文件的图标
5、点击每个文件夹进入该文件夹中,并且显示其所有的文件
xml布局,
1、TextView用于显示当前所在的位置。
2、ListView显示父文件夹中的所有文件


1、通过File的listFile()方法获取指定目录下的全部文件和文件夹
2、定义String currentParent 变量,为当前的父文件夹、File currentFiles()为当前的所有文件
3、将获取到的所有文件放在ListView中。
4、如果文件时个文件夹,那么显示文件夹的图标,否则显示文件的图标
5、点击每个文件夹进入该文件夹中,并且显示其所有的文件
xml布局,
1、TextView用于显示当前所在的位置。
2、ListView显示父文件夹中的所有文件
3、button按钮,返回上一级目录
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:id="@+id/currentsite"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/allFiles"
android:layout_below="@+id/currentsite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
<Button
android:id="@+id/reback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回上一级"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/mycotents"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
catalog.xm
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="@+id/fileimage"
android:layout_width="50dp"
android:layout_height="50dp"></ImageView>
<TextView
android:id="@+id/filename"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
package lzl.edu.com.sdcardexplore;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
TextView mCurrentSite,mycotents;
ListView mAllFiles;
Button mReback;
File currentParent = null; //父文件夹
final String sdPath ="/mnt/sdcard";//定义sd卡的路径
File[] currentFiles = new File[]{};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mCurrentSite = (TextView)findViewById(R.id.currentsite);
mAllFiles = (ListView)findViewById(R.id.allFiles);
mReback = (Button)findViewById(R.id.reback);
mycotents = (TextView)findViewById(R.id.mycotents);
mReback.setOnClickListener(new Reback());
//获取文件中的所有目录
getAllDirectory();
//为每个item设置点击事件,并产生相应的响应
mAllFiles.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/**
* 主要是判断 该子文件是否是文件夹,
* 1、如果是
* 1.1 如果存在子目录,则显示所有的子文件
* 1.2 如果不存在子目录,则出现提示信息
* 2、不是,则提示不能进入下个目录
*/
if (currentFiles[position].isFile()) { //是文件,则不能进入下一路径
//读取文件中的内容
try {
File file = new File(currentParent.getCanonicalPath()+"/"+currentFiles[position].getName());
Log.i("path------", currentParent.getCanonicalPath()+"/"+currentFiles[position].getName());
BufferedReader bufr = new BufferedReader( new FileReader(file));
String line = null;
StringBuffer sb = new StringBuffer();
while((line = bufr.readLine())!=null){
sb.append(line);
}
Intent intent = new Intent(MainActivity.this,GetContents.class);
intent.putExtra("content",sb.toString());
startActivity(intent);
bufr.close();
}catch (IOException e){
e.printStackTrace();
}
}else {
File temp[] = currentFiles[position].listFiles();
if(temp==null||temp.length==0){
Toast.makeText(MainActivity.this,"没有子目录了",Toast.LENGTH_LONG).show();
}else {
//显示该目下的所有文件,并将父目录设置成该目录
currentParent = currentFiles[position];
currentFiles = temp;
inflateListView(currentFiles); //更新文件
}
}
}
});
}
public void getAllDirectory(){
File sdFiles = new File(sdPath);
//1、判断手机是否插有sd卡
if(sdFiles.exists()){
currentParent = sdFiles; //设置当前文件夹为父文件夹
currentFiles = sdFiles.listFiles(); //获得所有的文件
inflateListView(currentFiles); //将所有的文件设置到listView中
}
}
//设置ListView中的布局
public void inflateListView( File[] currentFiles){
List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
//遍历每一个文件
for (int i=0;i<currentFiles.length;i++){
Map<String,Object> fileMap = new HashMap<String,Object>();
if(currentFiles[i].isDirectory()){//如果是文件夹,则给设置图片为文件夹图片
fileMap.put("icon",R.mipmap.folder_icon);
} else if(currentFiles[i].isFile()){ //如果是文件,则给设置图片为文件图片
fileMap.put("icon",R.mipmap.file_icon);
}
//将文件名称加入到Map集合中
Log.i("fileName----------",currentFiles[i].getName());
fileMap.put("fileName",currentFiles[i].getName());
//将map集合中的数据加入到list集合中
list.add(fileMap);
}
//采用SimpleAdapter类来加载listView中的数据
SimpleAdapter simpleAdapter = new SimpleAdapter(this,list,R.layout.catalog,
new String[]{"icon","fileName"},new int[]{R.id.fileimage,R.id.filename});
//设置适配器
mAllFiles.setAdapter(simpleAdapter);
//显示当前路径
try {
String fileSite = currentParent.getCanonicalPath();
mCurrentSite.setText(fileSite);
}catch (IOException e){
e.printStackTrace();
}
}
//返回上一级的点击事件
class Reback implements View.OnClickListener{
@Override
public void onClick(View v) {
/**点击按钮之后,判断是不是根目录
1.若是则,不能进行返回上一级
2.若不是,则可以返回上一级
*/
try {
if (currentParent.getCanonicalPath().equals("/")) {
Toast.makeText(MainActivity.this,"已经是根目录了",Toast.LENGTH_LONG).show();
Log.i("90909090","已经是根目录了");
return;
}else{
currentParent = currentParent.getParentFile();
File temp[] = currentParent.listFiles();
if(temp==null||temp.length==0){
Toast.makeText(MainActivity.this,"没有子目录了",Toast.LENGTH_LONG).show();
}else {
//显示该目下的所有文件,并将父目录设置成该目录
currentFiles = temp;
inflateListView(currentFiles); //更新文件
}
}
}catch (IOException e){
e.printStackTrace();
}
}
}
}
activity_get_contents.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="lzl.edu.com.sdcardexplore.GetContents">
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
GetContents.java
package lzl.edu.com.sdcardexplore;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class GetContents extends Activity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_contents);
textView = (TextView)findViewById(R.id.content);
Intent intent = getIntent();
String con = intent.getStringExtra("content");
textView.setText(con);
}
}