Android-简单的sdcard文件浏览

功能:能够浏览手机里面的文件夹和文件,代码灰常简单

 

先看布局

<LinearLayout 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:orientation="vertical"
    tools:context="com.example.fanlei.myservicedemo.FilePathActivity">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#000">
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="返回上一级"/>
    </android.support.v7.widget.Toolbar>
    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</LinearLayout>

主函数

  1 package com.example.fanlei.myservicedemo;
  2 
  3 import android.annotation.TargetApi;
  4 import android.os.Build;
  5 import android.os.Bundle;
  6 import android.os.Environment;
  7 import android.support.v7.app.ActionBarActivity;
  8 import android.view.View;
  9 import android.widget.AdapterView;
 10 import android.widget.ArrayAdapter;
 11 import android.widget.Button;
 12 import android.widget.ListView;
 13 import android.widget.Toast;
 14 import java.io.File;
 15 import java.util.ArrayList;
 16 import java.util.List;
 17 
 18 public class FilePathActivity extends ActionBarActivity {
 19 
 20 
 21     private ListView listView;
 22     private File currentParent;   //记录父文件夹
 23     private File[] currentFiles;  //记录当前路径下的所有文件的文件数组
 24     private List<String> fileName;//文件或文件夹名字
 25 
 26     private ArrayAdapter arrayAdapter;
 27     private Button button;
 28 
 29     @TargetApi(Build.VERSION_CODES.KITKAT)
 30     @Override
 31     protected void onCreate(Bundle savedInstanceState) {
 32         super.onCreate(savedInstanceState);
 33         setContentView(R.layout.activity_file_path);
 34 
 35         //判断一下有木有sdcard卡
 36         if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
 37             initView();
 38             initData();
 39             initEvent();
 40         }else {
 41             Toast.makeText(this,"无sdcard卡",Toast.LENGTH_LONG).show();
 42         }
 43 
 44     }
 45 
 46     private void initView(){
 47         listView = (ListView) findViewById(R.id.listView);
 48         button = (Button) findViewById(R.id.button);
 49     }
 50 
 51     private void initData(){
 52         currentParent = Environment.getExternalStorageDirectory();
 53         currentFiles = currentParent.listFiles();
 54         fileName = new ArrayList<>();
 55         List<String> temp = filesToFileName(currentFiles);
 56         fileName.addAll(temp);
 57         arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,fileName);
 58         listView.setAdapter(arrayAdapter);
 59     }
 60 
 61     private void initEvent(){
 62         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
 63             @Override
 64             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 65                 if (!currentFiles[position].isFile()){
 66                     File[] temp = currentFiles[position].listFiles();
 67                     if (temp == null || temp.length == 0){
 68                         Toast.makeText(FilePathActivity.this,"该文件下已无文件",Toast.LENGTH_LONG).show();
 69                     }else {
 70                         currentParent = currentFiles[position];
 71                         currentFiles = temp;
 72                         fileName.clear();
 73                         fileName.addAll(filesToFileName(currentFiles));
 74                         arrayAdapter.notifyDataSetChanged();
 75                     }
 76                 }
 77             }
 78         });
 79         //返回键
 80         button.setOnClickListener(new View.OnClickListener() {
 81             @Override
 82             public void onClick(View v) {
 83                 if (!currentParent.getPath().equals(Environment.getExternalStorageDirectory().getPath())){
 84                     currentParent = currentParent.getParentFile();
 85                     currentFiles = currentParent.listFiles();
 86                     fileName.clear();
 87                     fileName.addAll(filesToFileName(currentFiles));
 88                     arrayAdapter.notifyDataSetChanged();
 89                 }
 90             }
 91         });
 92     }
 93     //返回一个File的文件名的数组
 94     private List<String> filesToFileName(File[] files){
 95         List<String> fileName = new ArrayList<>();
 96         for (int i = 0;i < files.length;i++){
 97             if (!files[i].getName().substring(0,1).equals(".")){  //这个地方我把“.”开头的文件夹不让其显示
 98                 fileName.add(files[i].getName());
 99             }
100         }
101         return fileName;
102     }
103 }

 

posted @ 2015-10-28 16:18  灬布衣丶公爵丨  阅读(389)  评论(1编辑  收藏  举报