安卓--一个简单的SD卡文件浏览程序

一个简单的SD卡文件浏览程序。
本程序主要使用java.io中对于文件的一些操作,获取到手机上的SD卡之后,获取SD卡上的文件和文件夹,并将文件或文件夹的名字显示在ListView中,点击如果是文件夹,则显示该文件夹下面的文件,如果是文件,暂时不做任何操作,并且重写了点击返回键的方法。

java文件:
package com.example.zyf.sdcardfile;

import android.content.DialogInterface;
import android.os.Environment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BrowseFILES extends AppCompatActivity {

TextView parentFile;
    Button upper;
    ListView fileList;
    //记录当前的父文件夹
    File currentParent;
    //记录当前路径下的所有的文件数组
    File[] currentFiles;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browse_files);

        parentFile=(TextView)findViewById(R.id.parentFile);
        upper=(Button)findViewById(R.id.upper);
        fileList=(ListView)findViewById(R.id.files);




            File root=new File("/mnt/sdcard/");
        if (root.exists()) {
            currentParent = root;
            currentFiles = root.listFiles();
            inflateListView(currentFiles);
        }

        fileList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //如果用户单击的是文件
                if(currentFiles[position].isFile()){

                    Toast.makeText(BrowseFILES.this, "已经到达根目录", Toast.LENGTH_SHORT).show();

                }else{
                    //用户单击的是文件夹
                    File[] temp=currentFiles[position].listFiles();
                    if(temp==null || temp.length==0){
                        Toast.makeText(BrowseFILES.this, "文件夹下没有文件或者当前路径不可访问", Toast.LENGTH_SHORT).show();

                    }else{

                        //将当前获取到的这个文件夹设为付文件夹
                        currentParent=currentFiles[position];
                        //保存当前父文件夹的全部文件和文件夹
                        currentFiles=temp;
                        //在此更新ListView中要显示的内容
                        inflateListView(currentFiles);
                    }

                }

            }
        });
        //给上一级按钮添加事件
        upper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                    System.out.println("文件路径"+currentParent.getAbsolutePath().toString());
                    if(currentParent.getAbsolutePath().toString().equals("/mnt/sdcard")){

                        return;
                    }else{

                        //获取上一级目录
                        currentParent=currentParent.getParentFile();
                        //列出当前目录下所有文件
                        currentFiles=currentParent.listFiles();
                        //更新ListView
                        inflateListView(currentFiles);
                    }

                }

        });
    }

    private void inflateListView(File[] files) {

        //创建集合,显示当前文件夹下所有的文件
        List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();
        //给Map赋值
        for(int i = 0;i<files.length;i++){
            Map<String,Object> listItem=new HashMap<String, Object>();
            //如果是文件夹,显示文件夹的图片
            if(files[i].isDirectory()){
               listItem.put("icon",R.mipmap.folder);
            }else{
                listItem.put("icon",R.mipmap.file);

            }
            listItem.put("filename",files[i].getName());

            listItems.add(listItem);

        }

        SimpleAdapter simple=new SimpleAdapter(
                this,
                listItems,
                R.layout.fileadapter,
                new String[]{"icon","filename"},
                new int[]{R.id.fileImg,R.id.filename}

        );
        fileList.setAdapter(simple);
        try {
            parentFile.setText(currentParent.getCanonicalPath().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //重写点击返回键事件
    @Override
    public void onBackPressed() {
       // super.onBackPressed();

            if(currentParent.getAbsolutePath().toString().equals("/mnt/sdcard")){
                //如果返回到根目录了,创建一个DiaLog
                AlertDialog.Builder alert=new AlertDialog.Builder(this);
                alert.setTitle("退出提示");
                alert.setMessage("是否确认退出");
                alert.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                alert.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        finish();
                    }
                });
                alert.create();
                alert.show();

            }else{

                currentParent=currentParent.getParentFile();

    currentFiles=currentParent.listFiles();
                inflateListView(currentFiles);
            }



    }
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.zyf.sdcardfile.BrowseFILES">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    <TextView
        android:id="@+id/parentFile"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:background="@android:drawable/edit_text"
         />
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/upper"
            android:text="上一层"
            />

    </LinearLayout>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/files"

        />

</LinearLayout>
ListView的SimpleAdapter布局文件
<?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="40dp">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/fileImg"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="6"
        android:id="@+id/filename"
        />

</LinearLayout>
发现的问题:
问题探讨: 在对当前是否是根目录做比较的时候发现,调用getCanonicalPath() 不能准确的和给出的根目录做比较,所以,选择调用getAbsolutePath()方法,具体情况不明确。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值