歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android入門之文件系統操作

Android入門之文件系統操作——簡單的文件浏覽器

     

  1. import java.io.File;  
  2. import java.util.*;  
  3.   
  4. import android.app.Activity;  
  5. import android.content.Context;  
  6. import android.os.*;  
  7. import android.view.*;  
  8. import android.widget.*;  
  9. import android.widget.AdapterView.OnItemClickListener;  
  10. import android.widget.ImageView.ScaleType;  
  11.   
  12. public class FileBrowser extends Activity {  
  13.   
  14.     private ListView mainListView=null;  
  15.     private List<Map<String,Object>> list=null;  
  16.       
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         this.setTitle("文件浏覽器");  
  20.         mainListView=new ListView(this);  
  21.         setContentView(mainListView);  
  22.           
  23.         File file=Environment.getRootDirectory();  
  24.         String pathx=file.getAbsolutePath();  
  25.         this.setTitle(pathx);  
  26.         //android的總目錄就是"/"   
  27.         list_init("/");  
  28.     }  
  29.       
  30.     void list_init(String path){  
  31.         File file=new File(path);  
  32.         File[] fileList=file.listFiles();  
  33.         list=new ArrayList<Map<String,Object>>();  
  34.         Map<String,Object> item;    
  35.         item=new HashMap<String,Object>();   
  36.         if(path.equals("/")){  
  37.             item.put("ico",R.drawable.home);   
  38.             item.put("name","總目錄列表");    
  39.             item.put("path","/");    
  40.             list.add(item);  
  41.         }else{  
  42.             item.put("ico",R.drawable.back);   
  43.             item.put("name","返回上一級");    
  44.             item.put("path",file.getParent());    
  45.             list.add(item);  
  46.         }  
  47.         for(int i=0;i<fileList.length;i++){  
  48.             item=new HashMap<String,Object>();   
  49.             if(fileList[i].isDirectory()){  
  50.                 if(fileList[i].list().length<1){  
  51.                     item.put("ico",R.drawable.file1);  
  52.                 }else{  
  53.                     item.put("ico",R.drawable.file2);  
  54.                 }  
  55.             }else{  
  56.                 item.put("ico",R.drawable.content);   
  57.             }  
  58.             item.put("name",fileList[i].getName());    
  59.             item.put("path",fileList[i].getAbsolutePath());    
  60.             list.add(item);    
  61.         }  
  62.         MyAdapter ma=new MyAdapter(this,list);   
  63.         //mainListView=new ListView(this);   
  64.         mainListView.setAdapter(ma);   
  65.         mainListView.setOnItemClickListener(new OnItemClickListener(){  
  66.             public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {  
  67.                 if(arg2>0 && (Integer)(list.get(arg2).get("ico"))==R.drawable.content){  
  68.                     //非文件夾圖標,點擊無效   
  69.                 }else{  
  70.                     //打開下一級文件目錄列表   
  71.                     list_init((String)(list.get(arg2).get("path")));  
  72.                 }  
  73.             }  
  74.         });  
  75.         this.setTitle(path);  
  76.     }  
  77.       
  78.     public class MyAdapter extends BaseAdapter{    
  79.             
  80.         Context context=null;    
  81.         List<Map<String,Object>> list=null;    
  82.             
  83.         MyAdapter(Context context,List<Map<String,Object>> list){    
  84.             this.context=context;    
  85.             this.list=list;  
  86.         }    
  87.         public int getCount() {return list.size();}    
  88.         public Object getItem(int position) {return position;}    
  89.         public long getItemId(int position) {return position;}    
  90.         
  91.         public View getView(int position, View convertView, ViewGroup parent) {    
  92.             LinearLayout returnView=new LinearLayout(context);    
  93.             returnView.setLayoutParams(new ListView.LayoutParams(-1,-2));//注意:ListView.LayoutParams      
  94.             //圖標   
  95.             ImageView iv=new ImageView(context);    
  96.             LinearLayout.LayoutParams lp_iv=new LinearLayout.LayoutParams(-2,-2);      
  97.             lp_iv.rightMargin=10;  
  98.             iv.setLayoutParams(lp_iv);   
  99.             iv.setScaleType(ScaleType.CENTER_INSIDE);    
  100.             iv.setImageResource((Integer)((list.get(position)).get("ico")));    
  101.             returnView.addView(iv);  
  102.             //文件名    
  103.             TextView name=new TextView(context);    
  104.             LinearLayout.LayoutParams lp_tv=new LinearLayout.LayoutParams(-2,-2);   
  105.             name.setLayoutParams(lp_tv);    
  106.             name.setTextSize(name.getTextSize()+10);    
  107.             name.setText((String)(list.get(position).get("name")));  
  108.             returnView.addView(name);  
  109.             //   
  110.             return returnView;    
  111.         }    
  112.         
  113.     }   
  114. }  
Copyright © Linux教程網 All Rights Reserved