Android文件浏览器

一个简单的Android文件浏览器:

public class FileList extends ListActivity
{	
	private List<String> items = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.directory_list);
        fill(new File("/").listFiles());
    }
    
    @Override
	protected void onListItemClick(ListView l, View v, int position, long id) 
    {
		int selectionRowID = (int) id;
		if (selectionRowID == 0) 
		{
			fillWithRoot();
		}
		else 
		{
			File file = new File(items.get(selectionRowID));
			if (file.isDirectory())
			{
				fill(file.listFiles());
			}
			else
			{
				//AlertDialog.show(this, "Not a Directory","That's a file, not a directory", "Cancel", false);
				Intent result = new Intent();
				result.putExtra("filename", file.getName());
				setResult(RESULT_OK, result);
				finish();
			}			
		}
	}

    private void fillWithRoot() {
    	fill(new File("/").listFiles());
    }

	private void fill(File[] files) 
	{
		if(files == null)
			return;
		
		items = new ArrayList<String>();
		items.add(getString(R.string.to_top));
		for (File file : files)
			items.add(file.getName());
		
		ArrayAdapter<String> fileList = new ArrayAdapter<String>(this,
				R.layout.file_row, items);
		setListAdapter(fileList);
	}
}

其中的布局文件为

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	> 
	
	<ListView 
	android:id="@id/android:list"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	/>
	
	<TextView 
		android:id="@id/android:empty"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="@string/no_files"
	/>
</LinearLayout> 

需要添加的权限:

	<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值