欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-08-15 10:30:20
核心代码: file fatherfile = new file(path);file[] files = fatherfile.listfiles(); 效果图:...

核心代码:

file fatherfile = new file(path);file[] files = fatherfile.listfiles();

效果图:

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

实现这种列表式的目录,用直接读取目录下文件方法会比较简单,但是如果要根据文件类型从所有文件中分类,那就用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简易文件管理器实例(列表式文件目录)

以上这篇android简易文件管理器实例(列表式文件目录)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。