Android-简单的sdcard文件浏览

时间:2022-05-10 11:07:59

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

先看布局

<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>

主函数

 package com.example.fanlei.myservicedemo;

 import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
import java.util.List; public class FilePathActivity extends ActionBarActivity { private ListView listView;
private File currentParent; //记录父文件夹
private File[] currentFiles; //记录当前路径下的所有文件的文件数组
private List<String> fileName;//文件或文件夹名字 private ArrayAdapter arrayAdapter;
private Button button; @TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_path); //判断一下有木有sdcard卡
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
initView();
initData();
initEvent();
}else {
Toast.makeText(this,"无sdcard卡",Toast.LENGTH_LONG).show();
} } private void initView(){
listView = (ListView) findViewById(R.id.listView);
button = (Button) findViewById(R.id.button);
} private void initData(){
currentParent = Environment.getExternalStorageDirectory();
currentFiles = currentParent.listFiles();
fileName = new ArrayList<>();
List<String> temp = filesToFileName(currentFiles);
fileName.addAll(temp);
arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,fileName);
listView.setAdapter(arrayAdapter);
} private void initEvent(){
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (!currentFiles[position].isFile()){
File[] temp = currentFiles[position].listFiles();
if (temp == null || temp.length == 0){
Toast.makeText(FilePathActivity.this,"该文件下已无文件",Toast.LENGTH_LONG).show();
}else {
currentParent = currentFiles[position];
currentFiles = temp;
fileName.clear();
fileName.addAll(filesToFileName(currentFiles));
arrayAdapter.notifyDataSetChanged();
}
}
}
});
//返回键
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!currentParent.getPath().equals(Environment.getExternalStorageDirectory().getPath())){
currentParent = currentParent.getParentFile();
currentFiles = currentParent.listFiles();
fileName.clear();
fileName.addAll(filesToFileName(currentFiles));
arrayAdapter.notifyDataSetChanged();
}
}
});
}
//返回一个File的文件名的数组
private List<String> filesToFileName(File[] files){
List<String> fileName = new ArrayList<>();
for (int i = 0;i < files.length;i++){
if (!files[i].getName().substring(0,1).equals(".")){ //这个地方我把“.”开头的文件夹不让其显示
fileName.add(files[i].getName());
}
}
return fileName;
}
}