【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>    

原创粉丝点击