android 简易文件管理器 android简易文件管理器实例(列表式文件目录)
人气:0想了解android简易文件管理器实例(列表式文件目录)的相关内容吗,在本文为您仔细讲解android 简易文件管理器的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:简易文件管理器,下面大家一起来学习吧。
核心代码:
File fatherFile = new File(path);File[] files = fatherFile.listFiles();
效果图:
实现这种列表式的目录,用直接读取目录下文件方法会比较简单,但是如果要根据文件类型从所有文件中分类,那就用ContentProvider去查询数据库方式会更有效率;
实现代码:
FileListActivity.java
package com.example.d_readandwritetextfile; import java.io.File; import java.util.ArrayList; import com.example.d_readandwritetextfile.entity.FileEntity; import com.example.d_readandwritetextfile.entity.FileEntity.Type; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; /** * 文件列表 界面 * @author Administrator * */ public class FileListActivity extends Activity implements OnClickListener{ private ListView mListView; private Button btnComfirm; private MyFileAdapter mAdapter; private Context mContext; private File currentFile; String sdRootPath; private ArrayList<FileEntity> mList; private Handler mHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_filelist); mHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 1: if(mAdapter ==null){ mAdapter = new MyFileAdapter(mContext, mList); mListView.setAdapter(mAdapter); }else{ mAdapter.notifyDataSetChanged(); } break; case 2: break; default: break; } } }; mContext = this; mList = new ArrayList<>(); sdRootPath =Environment.getExternalStorageDirectory().getAbsolutePath(); currentFile = new File(sdRootPath); System.out.println(sdRootPath); initView(); getData(sdRootPath); } @Override public void onBackPressed() { // super.onBackPressed(); System.out.println("onBackPressed..."); if(sdRootPath.equals(currentFile.getAbsolutePath())){ System.out.println("已经到了根目录..."); return ; } String parentPath = currentFile.getParent(); currentFile = new File(parentPath); getData(parentPath); } private void initView() { mListView = (ListView) findViewById(R.id.listView1); btnComfirm = (Button) findViewById(R.id.button1); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { final FileEntity entity = mList.get(position); if(entity.getFileType() == Type.FLODER){ currentFile = new File(entity.getFilePath()); getData(entity.getFilePath()); }else if(entity.getFileType() == Type.FILE){ runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(mContext, entity.getFilePath()+" "+entity.getFileName(), 1).show(); } }); } } }); } private void getData(final String path) { new Thread(){ @Override public void run() { super.run(); findAllFiles(path); } }.start(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: setResult(100); finish(); break; default: break; } } /** * 查找path地址下所有文件 * @param path */ public void findAllFiles(String path) { mList.clear(); if(path ==null ||path.equals("")){ return; } File fatherFile = new File(path); File[] files = fatherFile.listFiles(); if (files != null && files.length > 0) { for (int i = 0; i < files.length; i++) { FileEntity entity = new FileEntity(); boolean isDirectory = files[i].isDirectory(); if(isDirectory ==true){ entity.setFileType(Type.FLODER); // entity.setFileName(files[i].getPath()); }else{ entity.setFileType(Type.FILE); } entity.setFileName(files[i].getName().toString()); entity.setFilePath(files[i].getAbsolutePath()); entity.setFileSize(files[i].length()+""); mList.add(entity); } } mHandler.sendEmptyMessage(1); } class MyFileAdapter extends BaseAdapter { private Context mContext; private ArrayList<FileEntity> mAList; private LayoutInflater mInflater; public MyFileAdapter(Context mContext, ArrayList<FileEntity> mList) { super(); this.mContext = mContext; this.mAList = mList; mInflater = LayoutInflater.from(mContext); } @Override public int getCount() { // TODO Auto-generated method stub return mAList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mAList.get(position); } @Override public long getItemId(int position) { return position; } @Override public int getItemViewType(int position) { if(mAList.get(position).getFileType() == Type.FLODER){ return 0; }else{ return 1; } } @Override public int getViewTypeCount() { return 2; } @Override public View getView(int position, View convertView, ViewGroup parent) { // System.out.println("position-->"+position+" ---convertView--"+convertView); ViewHolder holder = null; int type = getItemViewType(position); FileEntity entity = mAList.get(position); if(convertView == null){ holder = new ViewHolder(); switch (type) { case 0://folder convertView = mInflater.inflate(R.layout.item_listview, parent, false); holder.iv = (ImageView) convertView.findViewById(R.id.item_imageview); holder.tv = (TextView) convertView.findViewById(R.id.item_textview); break; case 1://file convertView = mInflater.inflate(R.layout.item_listview, parent, false); holder.iv = (ImageView) convertView.findViewById(R.id.item_imageview); holder.tv = (TextView) convertView.findViewById(R.id.item_textview); break; default: break; } convertView.setTag(holder); }else { holder = (ViewHolder) convertView.getTag(); } switch (type) { case 0: holder.iv.setImageResource(R.drawable.folder_125); holder.tv.setText(entity.getFileName()); break; case 1: holder.iv.setImageResource(R.drawable.file); holder.tv.setText(entity.getFileName()); break; default: break; } return convertView; } } class ViewHolder { ImageView iv; TextView tv; } }
FileEntity.java
package com.example.d_readandwritetextfile.entity; public class FileEntity { public enum Type{ FLODER,FILE } private String filePath; private String fileName; private String fileSize; private Type fileType; public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getFileSize() { return fileSize; } public void setFileSize(String fileSize) { this.fileSize = fileSize; } public Type getFileType() { return fileType; } public void setFileType(Type fileType) { this.fileType = fileType; } }
activity_filelist.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="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginRight="14dp" android:onClick="onClick" android:text="确定" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#666" android:layout_alignParentBottom="true" /> </RelativeLayout> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
item_listview.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="50dp" android:orientation="horizontal" > <ImageView android:id="@+id/item_imageview" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="10dp" android:src="@drawable/folder_125" android:scaleType="fitXY" /> <TextView android:id="@+id/item_textview" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:text="TextView" android:gravity="center" /> </LinearLayout>
以上这篇android简易文件管理器实例(列表式文件目录)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
加载全部内容