`

ListView带图标并且可以打开的文件浏览器

 
阅读更多

主程序:

 

private List<String> items = null;
	private List<String> paths = null;
	private String rootPath = "/";

	private TextView mPath;
	private ListView listView1;

	protected void onCreate(Bundle icicle) {

		super.onCreate(icicle);
		setContentView(R.layout.file_browser);

		mPath = (TextView) findViewById(R.id.textView1);
		listView1 = (ListView) findViewById(R.id.listView1);
		getFileDir(rootPath);
	}

	/* 取得文件架构的method */
	private void getFileDir(String filePath) {
		/* 设定目前所在路径 */
		mPath.setText(filePath);

		items = new ArrayList<String>();
		paths = new ArrayList<String>();
		File f = new File(filePath);
		File[] files = f.listFiles();

		if (!filePath.equals(rootPath)) {
			/* 第一笔设定为[回到根目录] */
			items.add("返回根目录");
			paths.add(rootPath);
			/* 第二笔设定为[回上层] */
			items.add("返回上一层");
			paths.add(f.getParent());
		}

		/* 将所有文件加入ArrayList中 */
		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			items.add(file.getName());
			paths.add(file.getPath());
		}

		/*
		 * 声明一ArrayAdapter,使用file_row这个Layout, 并将Adapter设定给此ListActivity
		 */
		MyListAdapter myListAdapter = new MyListAdapter(this ,items , paths);
		listView1.setAdapter(myListAdapter);

		listView1.setOnItemClickListener(new ListView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				File file = new File(paths.get(arg2));
				if (file.canRead()) {
					if (file.isDirectory()) {
						/* 如果是文件夹就再进去读取 */
						getFileDir(paths.get(arg2));
					} else {
						openFile(file);//自定义打开文件的方法
					}
				} else {
					/* 弹出AlertDialog显示权限不足 */
					new AlertDialog.Builder(IconFileBrowser.this)
							.setTitle("Message")
							.setMessage("权限不足!")
							.setPositiveButton("OK",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int which) {
										}
									}).show();
				}
			}

		});

 MyListAdapter.class

 

public class MyListAdapter extends BaseAdapter{
		
		private Context context;
		private List<String> fileName , filePath;
		
		public MyListAdapter(Context context , List<String> fileName , List<String> filePath){
			this.context = context;
			this.fileName = fileName;
			this.filePath = filePath;
		}

		public int getCount() {
			return fileName.size();
		}

		public Object getItem(int position) {
			return fileName.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(context);//LayoutInflater inflater = getLayoutInflater();
			View layout = inflater.inflate(R.layout.list_item3 , null);
			ImageView image = (ImageView) layout.findViewById(R.id.imageView1);
			File file = new File(filePath.get(position));
			if(file.isDirectory()){
				image.setImageResource(R.drawable.folder);
			}
			else image.setImageResource(R.drawable.doc);
			TextView title = (TextView) layout.findViewById(R.id.textView1);
			title.setText(fileName.get(position));
			return layout;
		}
		
	}

 list_item3.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:orientation="vertical">
	<LinearLayout
		android:layout_width="fill_parent"
		android:id="@+id/linearLayout1"
		android:layout_height="wrap_content">
		<ImageView
			android:id="@+id/imageView1"
			android:src="@drawable/icon"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"></ImageView>
		<TextView
			android:text="TextView"
			android:id="@+id/textView1"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"></TextView>
	</LinearLayout>
</LinearLayout>

 识别文件类型并打开

 

private void openFile(File f) {
		Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setAction(android.content.Intent.ACTION_VIEW);

		/* 调用getMIMEType()来取得MimeType */
		String type = getMIMEType(f);
		/* 设定intent的file与MimeType */
		intent.setDataAndType(Uri.fromFile(f), type);
		startActivity(intent);
	}

	/* 判断文件MimeType的method */
	private String getMIMEType(File f) {
		String type = "";
		String fName = f.getName();
		/* 取得扩展名 */
		String end = fName
				.substring(fName.lastIndexOf(".") + 1, fName.length())
				.toLowerCase();

		/* 依扩展名的类型决定MimeType */
		if (end.equals("m4a") || end.equals("mp3") || end.equals("mid")
				|| end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
			type = "audio";
		} else if (end.equals("3gp") || end.equals("mp4")) {
			type = "video";
		} else if (end.equals("jpg") || end.equals("gif") || end.equals("png")
				|| end.equals("jpeg") || end.equals("bmp")) {
			type = "image";
		} else {
			type = "*";
		}
		/* 如果无法直接打开,就弹出软件列表给用户选择 */
		type += "/*";
		return type;
	}
 

重命名文件

 

file.renameTo(new File(newPath));

 删除文件

 

file.delete();
 



 

 

 

  • 大小: 37.5 KB
  • 大小: 31.7 KB
  • 大小: 30.1 KB
  • 大小: 47.9 KB
分享到:
评论

相关推荐

    c# winform ListView实现图片浏览

    在C# WinForm开发中,ListView控件是一个非常常用且功能强大的组件,它可以用来展示列表式的数据,并且可以通过自定义来实现多种展示效果。在这个特定的案例中,我们讨论的是如何利用ListView控件来实现图片浏览的...

    Qml写的文件浏览器

    10. **多平台支持**:作为Qt的一部分,QML编写的程序可以在多种操作系统上运行,包括Windows、Linux、macOS、Android和iOS,这意味着这个文件浏览器可以跨平台使用。 通过学习和实践这个Qml写的文件浏览器项目,...

    ListView可扩展例程

    - 小图标和大图标视图:主要用于展示图形元素,如文件图标,通常用于文件浏览器等应用。 - 详细信息视图:除了列数据外,还可以显示额外的描述信息,例如文件的大小、日期等。 4. **添加数据到ListView** - 使用...

    android文件系统浏览器

    每个文件或目录项可以是一个自定义的ViewHolder,包含文件名、图标等信息。点击文件时,根据文件类型决定执行何种操作:打开文本文件,可以调用内置或第三方阅读器;点击图片,则展示图片预览;选择文件夹则更新当前...

    c#2010制作高仿winxp文件浏览器

    描述中的“可视化功能一应俱全”意味着该文件浏览器不仅具备基本的浏览、打开、新建、删除等文件操作,还可能包括复制、粘贴、重命名、搜索等高级功能。此外,强调“图标都是用心找的”,这表明开发者在用户体验上...

    VB.NET资源管理器带图标+文档说明

    "VB.NET资源管理器带图标+文档说明"项目提供了一个实现这一功能的实例,结合了树形目录(TreeView)和列表视图(ListView)控件,以显示文件系统的结构,并且带有图标,增强了用户体验。此外,通过集成SHGETFILEINFO...

    Android文件浏览器实现

    本文将深入探讨如何实现一个基于ListView的Android文件浏览器。首先,我们从标题和描述出发,理解我们的目标是创建一个使用ListView组件来展示文件和目录的应用。 ### 1. Android 文件操作权限 在Android中,任何...

    ListView控件工程案例

    在本工程案例中,我们将深入探讨如何使用C#语言结合ListView控件来实现一个功能丰富的文件浏览器。 首先,`ListView`控件在C#中提供了多种视图模式,包括“大图标”、“小图标”、“平铺”、“列表”和“详细信息”...

    Android自定义文件浏览器简单demo项目

    在Android中,文件浏览器通常需要自定义ListView或RecyclerView来展示文件列表。项目中可能包含了自定义Adapter,用于将文件信息(如名称、类型、大小等)绑定到视图上。此外,可能还设计了自定义的文件图标,以便...

    android文件浏览器源代码

    `ArrayList`和`SimpleAdapter`结合`ListView`展示文件列表,`Comparator`可以自定义文件排序规则。 `BufferedInputStream`和`BufferedOutputStream`提高读写效率,`ProgressDialog`通过`Handler`和`Looper`在后台...

    基于android的文件浏览器开发与实现

    例如,图片文件可以使用系统默认的图片查看器,而文档则可能需要第三方应用。复制和移动操作涉及文件或目录的剪贴板功能,可以使用`ContentProvider`来实现跨应用的数据传输。删除和重命名则直接调用`File`类的相关...

    演示使用listview控制作出资源管理器中目录树中的效果 (7KB)...

    在本文中,我们将深入探讨如何使用ListView控件在Windows应用程序中实现类似资源管理器中目录树的效果。...通过解析和理解这些文件,开发者可以学习到如何在Windows Forms应用中创建一个功能丰富的文件浏览器界面。

    Android手机SD卡文件浏览器

    文件浏览器通常具备的基本功能包括查看文件列表、打开文件、创建新文件夹、重命名、移动、复制、删除等。 对于"Android手机SD卡文件浏览器",其主要功能实现涉及以下几个技术点: 1. **文件遍历**:通过Java的`...

    Android-FileBrowser简易文件浏览器app

    这需要用到`MIME类型`识别,可以通过`MimeTypeMap`获取文件的MIME类型,并根据类型设置不同的图标和操作选项。 4. 权限管理:自Android 6.0(API级别23)引入运行时权限,开发者需要在运行时请求`READ_EXTERNAL_...

    C#中窗口listview全部代码

    在C#编程中,ListView控件是一个非常常用且功能强大的组件,它允许用户以列表的形式展示数据,可以用于创建各种类型的界面,如文件浏览器、数据库查看器等。本教程将详细解析"C#中窗口listview全部代码"所涵盖的知识...

    android文件浏览器

    描述中的“简单的文件浏览器代码”意味着它可能包含基础的文件操作逻辑,如打开、浏览目录结构、选择文件等,代码可能比较直观,适合初学者理解。这样的项目通常会用到Android SDK中的关键组件和API,例如`Intent`...

    android实现SDcard浏览器

    对于打开文件,可以使用`Intent`来调用其他能处理该文件类型的第三方应用。 对于文件操作,Android提供了`java.io.File`类来执行常见的文件操作,如读取、写入、创建、删除等。在SD卡浏览器中,我们可能需要实现...

    Android 仿ES界面文件浏览器源码.zip

    这包括创建新的目录、读取目录下的文件列表、打开文件等。此外,还会涉及到异步加载技术,比如使用AsyncTask,以保证在遍历大量文件时不会阻塞主线程,提供良好的用户体验。 2. **UI设计**:此项目中的界面设计借鉴...

    android 开发的文件浏览器软件

    使用XML布局文件设计界面,可以包含一个ListView或RecyclerView来展示文件列表,以及搜索框、路径显示栏等元素。 3. **文件系统的访问**:Android提供了java.io和java.nio包来处理文件操作。你需要使用`File`类来...

    打开文件夹并设置文件焦点

    例如,使用`ShellExecute`函数可以在Windows中打开文件夹,而使用各种控件(如`ListView`或`TreeView`)的API可以设置文件焦点。同时,现代图形用户界面(GUI)库如Qt或wxWidgets也提供了方便的方法来实现这些功能。...

Global site tag (gtag.js) - Google Analytics