SD卡文件浏览器

来源:互联网 发布:最新淘宝优惠券 编辑:程序博客网 时间:2024/05/22 03:03

    SD卡文件浏览器,主要练习:如何获取SD卡的文件目录。

    由于真机带有内置SD卡,

      File root = Environment.getExternalStorageDirectory(); 
            这条语句获取到的是内置SD卡的文件目录,外置SD卡(真正的SD卡)文件目录并没有获取到。

    获取外置SD卡文件目录的方法参考:获取外置SD卡路径的方法

    现有代码(获取到的是内置SD卡)如下:


布局文件:

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"     >    <TextView        android:id="@+id/path"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_alignParentTop="true" />        <ListView        android:id="@+id/listView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:divider="#000"        android:dividerHeight="1px"        android:layout_below="@+id/path"        />        <Button         android:id="@+id/parent"        android:layout_width="35dp"        android:layout_height="35dp"        android:layout_centerHorizontal="true"        android:layout_alignParentBottom="true"        android:background="@drawable/parent"        />    </RelativeLayout>


line.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >        <ImageView        android:id="@+id/file_icon"        android:layout_width="40dp"        android:layout_height="40dp"        />    <TextView        android:id="@+id/file_name"        android:layout_width="match_parent"        android:layout_height="40dp"        /></LinearLayout>

Java部分代码:

MainActivity.java:

package com.example.sdfileexplorer;import java.io.File;import java.io.IOException;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.os.Environment;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 Activity {ListView listView;TextView textView;File currentParent;File[] currenFiles;String finalPath;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        listView = (ListView) findViewById(R.id.listView);        textView = (TextView) findViewById(R.id.path);        //获取系统的SD卡的目录        File root = Environment.getExternalStorageDirectory();        //如果SD卡存在        if (root.exists()) {        currentParent = root;        currenFiles = root.listFiles();        //获得SD卡的路径,保存为String类型        try {finalPath = root.getCanonicalPath();} catch (IOException e) {e.printStackTrace();}        //使用当前目录下的全部文件、文件夹来填充ListView        inflateListView(currenFiles);        }        //为ListView列表项的单击事件绑定监听器        listView.setOnItemClickListener(new OnItemClickListener() {        @Override        public void onItemClick(AdapterView<?> parent, View view, int position,        long id) {        //用户单击了文件,直接返回,不做任何处理        if (currenFiles[position].isFile()){        return;        }        //获取用户单击的文件夹下的所有文件        File[] tmp = currenFiles[position].listFiles();        if (tmp ==null || tmp.length == 0) {        Toast.makeText(MainActivity.this, "当前路径不可访问或该路径下没有文件", Toast.LENGTH_SHORT).show();        }        else {        //获取用户单击的列表项对应的文件夹,设为当前的父文件夹        currentParent = currenFiles[position];        //保存当前的父文件夹内的全部文件和文件夹        currenFiles = tmp;        //再次更新ListView        inflateListView(currenFiles);        }        }});        //获取上一级目录的按钮        Button parent = (Button) findViewById(R.id.parent);        parent.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View source) {try {if (currentParent.getCanonicalPath().equals(finalPath)) {Toast.makeText(MainActivity.this, "已经到底了", Toast.LENGTH_SHORT).show();}if (!currentParent.getCanonicalPath().equals(finalPath)) {//获取上一级目录currentParent = currentParent.getParentFile();currenFiles = currentParent.listFiles();inflateListView(currenFiles);}} catch (IOException e) {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是文件夹使用folder图标,否则使用file图标    if (files[i].isDirectory()) {    listItem.put("icon", R.drawable.folder);    }    else {    listItem.put("icon", R.drawable.file);    }    listItem.put("name", files[i].getName());    listItems.add(listItem);    }    //创建一个SimpleAdapter    SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems,R.layout.line, new String[]{"icon","name"}, new int[]{R.id.file_icon,R.id.file_name});    listView.setAdapter(simpleAdapter);    try {    textView.setText("当前路径为:"+currentParent.getCanonicalPath());} catch (IOException e) {e.printStackTrace();}        }}


0 0