Android 文件浏览控件

//这种方式只能浏览普通用户有权限的文件。
package talent.fm;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class FileView extends ListView{
	private FileAdapter browser;
	
	public interface OnPathChanged{
		abstract boolean onChanged(String old,String path);
	}
	public void SetOnPathChangedListener(OnPathChanged listener){
		if(browser==null) return;
		browser.onChanged = listener;
	}
	public FileView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	public FileView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.FileView);             
        String path = a.getString(R.styleable.FileView_path);  
        a.recycle();
        if(path!=null)
        	browser.SetPath(path);
    }
	public FileView(Context context) {
		super(context);
		init();
	}
	private void init(){
		browser = new FileAdapter();
		setAdapter(browser);
        setOnItemClickListener(onItemClick);
	}
    private OnItemClickListener onItemClick = new OnItemClickListener(){
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
			//com.Logi("item:"+arg2);
			if(!browser.OpenChild(arg2)){
				com.Toast(arg1.getContext(),"folder is empty or can't open");
				return;
			}
		}
    };
    /**goto parent folder,return false if no parent folder.*/
    public boolean Up(){
    	return browser.Up();
    }
    public boolean SetPath(String path){
    	return browser.SetPath(path);
    }
    public String GetPath(){
    	return browser.GetPath();
    }
}
class FileAdapter extends BaseAdapter{
	private File folder = File.listRoots()[0];
	private List<File> contents = new ArrayList<File>();
	protected	FileView.OnPathChanged onChanged;
	com.SU su = new com.SU();

	public FileAdapter(){
		su.Open();
		initContents(null);
	}
	@Override
	public int getCount() {
		return contents.size();
	}
	/**return a File Object*/
	@Override
	public Object getItem(int position) {
		if(position>=contents.size()) return null;
		return contents.get(position);
	}
	@Override
	public long getItemId(int position) {
		return 0;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
	        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
	        convertView = inflater.inflate(R.layout.item_explorer,null);
		} 
		
		ImageView iv = (ImageView)convertView.findViewById(R.id.explorer_iv_image);
        int icon;
        File file = contents.get(position);
        if(file.isFile()) icon = R.drawable.icon_file;
        else if(file.isDirectory()) icon = R.drawable.icon_folder;
        else icon = R.drawable.icon_locked;
		iv.setImageResource(icon);
		
		TextView tv = (TextView)convertView.findViewById(R.id.explorer_iv_text);
		tv.setText(file.getName());
		
		;
		return convertView;
	}
	public boolean SetPath(String path){
		File file = new File(path);
		return initContents(file);
	}
	public String GetPath(){
		return folder.getPath();
	}
	public boolean OpenChild(int index){
		if(index>=contents.size()) return false;
		File sub = contents.get(index);
		if(!sub.isDirectory()) return false;
		return initContents(sub);
	}
	public boolean Up(){
		if(folder==null) return false;
		File root = folder.getParentFile();
		if(root==null) return false;
		return initContents(root);
	}
	private boolean initContents(File parent){
		if(parent==null){
			//this function return a File list that only one element of root folder.
			parent = File.listRoots()[0];
		}
		if(onChanged!=null){
			String old = "",cur = "";
			if(folder!=null)
				old = folder.getPath();
			cur = parent.getPath();
			if(onChanged.onChanged(old,cur)) return false;
		}
		//get parent contents.
		try {
			writeChmod(parent.getPath());
			File[] fs = parent.listFiles();
			contents.clear();
			folder = parent;
			resortByName(fs);
		} catch (SecurityException e){
			e.printStackTrace();
			return false;
		}
		notifyDataSetChanged();
		return true;
	}
	private void writeChmod(String file){
		String cmd = "chmod 777 " + file;
		su.Run(cmd);
	}
	private void resortByName(File[] ct){
		if(ct==null) return;
		Map<String,File> mapD = new HashMap<String,File>();//directory
		Map<String,File> mapF = new HashMap<String,File>();//file
		Map<String,File> mapU = new HashMap<String,File>();//unkown
		for(int i=0;i<ct.length;i++){
			File file = ct[i];
			String name = file.getName();
			if(file.isDirectory()) mapD.put(name,file);
			else if(file.isFile()) mapF.put(name,file);
			else mapU.put(name,file);
		}
		sortName(mapD);//sort folder;
		sortName(mapF);//sort file;
		sortName(mapU);//sort unkown;
	}
	private void sortName(Map<String,File> map){
		String[] ns = new String[map.size()];
		Set<String> set = map.keySet();
		ns = set.toArray(ns);
		Arrays.sort(ns);
		for(int i=0;i<ns.length;i++){
			contents.add(map.get(ns[i]));
		}
	}
}
//this is second edition,code is less
package talent.fm;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class FileBrowser extends ListView{
	private BrowserAdapter browser;
	
	public interface OnPathChanged{
		abstract boolean onChanged(String old,String path);
	}
	public void SetOnPathChangedListener(OnPathChanged listener){
		if(browser==null) return;
		browser.onChanged = listener;
	}
	public FileBrowser(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	public FileBrowser(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.FileView);             
        String path = a.getString(R.styleable.FileView_path);  
        a.recycle();
        if(path!=null)
        	browser.SetPath(path);
    }
	public FileBrowser(Context context) {
		super(context);
		init();
	}
	private void init(){
		browser = new BrowserAdapter();
		setAdapter(browser);
        setOnItemClickListener(onItemClick);
	}
    private OnItemClickListener onItemClick = new OnItemClickListener(){
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
			//com.Logi("item:"+arg2);
			if(!browser.OpenChild(arg2)){
				com.Toast(arg1.getContext(),"folder is empty or can't open");
				return;
			}
		}
    };
    /**goto parent folder,return false if no parent folder.*/
    public boolean Up(){
    	return browser.Up();
    }
    public void SetPath(String path){
    	browser.SetPath(path);
    }
    public String GetPath(){
    	return browser.GetPath();
    }
}
class BrowserAdapter extends BaseAdapter{
	private String path = "/";
	private List<FileInfo> contents = new ArrayList<FileInfo>();
	protected	FileBrowser.OnPathChanged onChanged;
    Console cs = new Console();
    private class FileInfo{
    	String	Name;
    	int		IconId;
    	FileInfo(String name,int id){
    		Name = name;
    		IconId = id;
    	}
    	boolean IsFolder(){
    		return R.drawable.icon_folder==IconId;
    	}
    }
	public BrowserAdapter(){
	}
	@Override
	public int getCount() {
		return contents.size();
	}
	/**return a File Object*/
	@Override
	public Object getItem(int position) {
		if(position>=contents.size()) return null;
		return contents.get(position);
	}
	@Override
	public long getItemId(int position) {
		return 0;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
	        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
	        convertView = inflater.inflate(R.layout.item_explorer,null);
		} 
        
		FileInfo fi = contents.get(position);
		
		ImageView iv = (ImageView)convertView.findViewById(R.id.explorer_iv_image);
		iv.setImageResource(fi.IconId);
		
		TextView tv = (TextView)convertView.findViewById(R.id.explorer_iv_text);
		tv.setText(fi.Name);

		return convertView;
	}
	public boolean SetPath(String path){
		if(path==null||path.length()<1)
			path = "/";
		File file = new File(path);
		if(!file.isDirectory()){
			com.Logw("FileBrowser.SetPath: path is not a directory");
			return false;
		}
		contents.clear();
		if(onChanged!=null) onChanged.onChanged(this.path,path);
		this.path = path;

		File[] fl = file.listFiles();
		if(fl==null){
			notifyDataSetChanged();
			return true;
		}
		Arrays.sort(fl);
		folderInsertPos = 0;
		for(int i=0;i<fl.length;i++){
			insertByFile(fl[i]);
		}
		notifyDataSetChanged();
		return true;
	}
	public String GetPath(){
		return path;
	}
	public boolean OpenChild(int index){
		if(index<0||index>=contents.size()) return false;
		FileInfo fi = contents.get(index);
		if(!fi.IsFolder()) return false;
		if(path.equals("/")) path = "";
		return SetPath(path+"/"+fi.Name);
	}
	public boolean Up(){
		File file = new File(path);
		File root = file.getParentFile();
		if(root==null){
			com.Logw("FileBrowser.Up: no parent path");
			return false;
		}
		SetPath(root.getPath());
		return true;
	}
	private int folderInsertPos = 0;
	/**insert FileInfo object in contents set,and position by name*/
	private void insertByFile(File file){
		int icon;
		if(file.isDirectory()){
			icon = R.drawable.icon_folder;
			contents.add(folderInsertPos,new FileInfo(file.getName(),icon));
			folderInsertPos++;
		}
		else{
	        if(file.isFile()){
	        	icon = R.drawable.icon_file;
	        }
	        else{
	        	icon = R.drawable.icon_script;
	        }
			contents.add(new FileInfo(file.getName(),icon));
		}
	}
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值