android:查找手机所有文件,进行浏览(文件浏览器)(13)

//查找手机所有文件,进行浏览(文件浏览器)
public class MainActivity extends Activity {
	private ListView listview;
	private TextView showPath;
	private SimpleAdapter adapter;
	private File file;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.listview = (ListView) this.findViewById(R.id.listview);
		this.showPath = (TextView) this.findViewById(R.id.showPath);
		//获取sd卡中的根路径
		String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
		file = new File(sdPath);
		findFileFillListview(file);//查找文件的方法

	}

	public void findFileFillListview(File file) {
		final File[] files = file.listFiles();
		// 准备数据
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < files.length; i++) {
			Map<String, Object> fileMap = new HashMap<String, Object>();
			if (files[i].isDirectory()) {
				fileMap.put("fileImage", R.drawable.folder);// 目录的图标
				fileMap.put("filename", files[i].getName());
			}
			if (files[i].isFile()) {
				fileMap.put("fileImage", R.drawable.file);// 文件的图标
				fileMap.put("filename", files[i].getName());// 这句有重复,可以把它提到外面去
			}
			// fileMap.put("filename", files[i].getName());
			data.add(fileMap);
		}
		//适配器关联数据
		adapter = new SimpleAdapter(this, data, R.layout.listviewitem,
				new String[] { "fileImage", "filename" }, new int[] {
						R.id.listview_image, R.id.listview_path });
		listview.setAdapter(adapter);
		showPath.setText(file.getAbsolutePath());
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 点击可以查看文件,如果是目录,再进行判断
				if (files[position].isDirectory()) {
					File[] filesub = files[position].listFiles();
					if (filesub.length == 0 || filesub == null) {
						Toast.makeText(MainActivity.this, "该目录为空!", 200).show();
					} else {
						findFileFillListview(files[position]);
					}
				} else {// 如果是文件进行打开浏览
					Intent intent = new Intent();
					intent.setAction(Intent.ACTION_VIEW);
					String extension = MimeTypeMap
							.getFileExtensionFromUrl(files[position]
									.getAbsolutePath());
									// 获取文件的扩展名
					if (extension.equals("jpg") || extension.equals("png")
							|| extension.equals("png")
							|| extension.equals("bmp")
							|| extension.equals("gif")
							|| extension.equals("jpeg")) {
						intent.setDataAndType(Uri.fromFile(files[position]),
								"image/*");

					} else if (extension.equals("mp3")
							|| extension.equals("png")
							|| extension.equals("mp4")
							|| extension.equals("mpg")
							|| extension.equals("avi")
							|| extension.equals("3gp")
							|| extension.equals("mpeg")) {
						intent.setDataAndType(Uri.fromFile(files[position]),
								"video/*");
					} else if (extension.equals("au")
							|| extension.equals("mid")) {
						intent.setDataAndType(Uri.fromFile(files[position]),
								"video/*");

					} else if (extension.equals("txt")
							|| extension.equals("xml")
							|| extension.equals("log")
							|| extension.equals("html")) {
						intent.setDataAndType(Uri.fromFile(files[position]),
								"text/*");
					} else {
						intent.setDataAndType(Uri.fromFile(files[position]),
								"text/*");
					}
					startActivity(intent);
				}
			}
		});
	}

	public void checkButton(View view) {
		if (!file.getAbsoluteFile().equals(Environment.getExternalStorageDirectory().getAbsolutePath())) {
			findFileFillListview(file.getParentFile());
			// 该按钮判断如果不等于绝对路径,获取父文件目录
		}
		/*
		 * switch (view.getId()) { case R.id.homeButtom: if
		 * (!file.getAbsoluteFile().equals(FileUtils.getSDCRRDDIR())) {
		 * findFileFillListview(file.getParentFile()); } break;
		 * 
		 * }
		 */
	}
}
//主布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/showPath"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000"
        android:textColor="#fff"
        android:textSize="20sp" />

    <ImageView
        android:id="@+id/homeButtom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:onClick="checkButton"
        android:src="@drawable/home" />

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

//listview的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/listview_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/listview_path"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#88000000"
        android:textColor="#fff"
        android:textSize="20sp" />

</LinearLayout>


转载于:https://my.oschina.net/u/2541146/blog/608638

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值