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>
相關文章
相關標籤/搜索