android简易文件管理器实例(列表式文件目录)

android简易文件管理器实例(列表式文件目录),第1张

概述核心代码:FilefatherFile=newFile(path);File[]files=fatherFile.listFiles();效果图:实现这种列表式的目录,用直接读取目录下文件方法会比较简单,但是如果要根据文件类型从所有文件中分类,那就用ContentPr

核心代码:

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,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简易文件管理器实例(列表式文件目录)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的android简易文件管理器实例(列表式文件目录)全部内容,希望文章能够帮你解决android简易文件管理器实例(列表式文件目录)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1146304.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存