【Android小范例教程】【1】查找程序根目录下的文件
来源:互联网 发布:alt 左键 ubuntu 编辑:程序博客网 时间:2024/06/08 04:22
【Android小范例教程】
第1期
android用户可以很随意的访问系统的所有文件,如果要实现类似于Windows文件资源管理器,要怎么做呢?其实,只要运用Java/IO中的java.io.File对象,搭配Android的ListActivity,就可以很轻松的实现手机版的文件资源管理器了。
代码如下:
package irdc.ex04_21;import java.io.File;import java.util.ArrayList;import java.util.List;import android.app.AlertDialog;import android.app.ListActivity;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.TextView;public class EX04_21 extends ListActivity{ /* items每个文件 paths对应的每个路径 rootPath */ private List<String> items=null; private List<String> paths=null; private String rootPath="/"; private TextView mPath; /** Called when the activity is first created. */ @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); mPath=(TextView)findViewById(R.id.mPath); getFileDir(rootPath); } /* 获取 该目录下的文件以及文件夹*/ 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("Back to "+rootPath); paths.add(rootPath); items.add("Back to ../"); paths.add(f.getParent()); } for(int i=0;i<files.length;i++) { File file=files[i]; items.add(file.getName()); paths.add(file.getPath()); } ArrayAdapter<String> fileList = new ArrayAdapter<String>(this,R.layout.file_row, items); setListAdapter(fileList); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { File file = new File(paths.get(position)); if (file.isDirectory()) { getFileDir(paths.get(position)); } else { new AlertDialog.Builder(this).setIcon(R.drawable.icon) .setTitle("["+file.getName()+"] is File!") .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int whichButton) { } }).show(); } }}
其中,main.xml代码如下:
<?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" android:orientation="vertical" > <TextView android:id="@+id/mPath" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5px" android:textSize="18sp" android:textColor="@drawable/blue" > </TextView> <ListView android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ListView> </LinearLayout>
file_row.xml
<?xml version="1.0" encoding="utf-8"?> <TextView android:id="@+id/text1" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="20px" android:textSize="14sp" > </TextView>
- 【Android小范例教程】【1】查找程序根目录下的文件
- Android学习之查找程序根目录下的所有文件
- Android--查找程序根目录下所有文件/Java IO操作
- 查找程序根目录下所有文件(Java IO与ListActivity的结合)
- 忙里偷闲写的小例子---读取android根目录下的文件或文件夹
- 查找程序根目录下所有文件-Java i/o与ListActivity
- 写小程序接口时phpcms中根目录下添加一个接口文件中
- 一个读取android根目录下的文件夹或文件
- 读取android根目录下的文件或文件夹
- 读取android根目录下的文件或文件夹
- 读取android根目录下的文件或文件夹
- android 获取sd卡根目录下的指定文件
- LabVIEW查找范例教程
- Java文件查找小程序
- proc文件系统探索 之 根目录下的文件[1]
- 查找域名的根目录
- Tomcat根目录下各个文件的作用
- linux根目录下的各个文件简介
- Android多任务下载,使用Notification显示、更新进度条
- Unbuntu常用的进程管理命令
- Tour in the Castle zoj3256
- core dump
- android实习程序 ——短信
- 【Android小范例教程】【1】查找程序根目录下的文件
- time_wait和close_wait(面试时被问到)
- 用GDB调试程序(三)
- Sencha Touch中使用标准LocalStorage
- UILocalNotification之soundName
- Poj 3321 Apple Tree(树变序列+时间戳+树状数组)
- 用GDB调试程序(四)
- postfix实现邮件发送的简单配置
- Vim配置与gcc常用命令