Building an Android FileBrowser (list-based)

来源:互联网 发布:乐视手机mac怎么修改 编辑:程序博客网 时间:2024/06/02 00:14
The Full Source:

res/layout/file_row.xml


  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <TextView
  3.         xmlns:android="http://schemas.android.com/apk/res/android"
  4.         android:layout_width="fill_parent"
  5.         android:layout_height="wrap_content"
  6. />
res/values/strings.xml


  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3.     <stringname="app_name">Android File Browser - by anddev.org</string>
  4.     <stringname="up_one_level">..</string>
  5.     <stringname="current_dir">.</string>
  6. </resources>
  1. package org.anddev.android.filebrowser;
  2.  
  3. import java.io.File;
  4. import java.net.URISyntaxException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. import android.app.AlertDialog;
  9. import android.app.ListActivity;
  10. import android.content.DialogInterface;
  11. import android.content.Intent;
  12. import android.content.DialogInterface.OnClickListener;
  13. import android.net.ContentURI;
  14. import android.os.Bundle;
  15. import android.view.View;
  16. import android.widget.ArrayAdapter;
  17. import android.widget.ListView;
  18.  
  19. public class AndroidFileBrowser extends ListActivity{
  20.        
  21.         private enum DISPLAYMODE{ ABSOLUTE, RELATIVE;}
  22.  
  23.         private final DISPLAYMODE displayMode = DISPLAYMODE.ABSOLUTE;
  24.         private List<String> directoryEntries= new ArrayList<String>();
  25.         private File currentDirectory = new File("/");
  26.  
  27.         /** Called when the activity is first created. */
  28.         @Override
  29.         public void onCreate(Bundle icicle){
  30.                 super.onCreate(icicle);
  31.                 // setContentView() gets called within the next line,
  32.                 // so we do not need it here.
  33.                 browseToRoot();
  34.         }
  35.        
  36.         /**
  37.          * This function browses to the
  38.          * root-directory of the file-system.
  39.          */
  40.         private void browseToRoot(){
  41.                 browseTo(newFile("/"));
  42.     }
  43.        
  44.         /**
  45.          * This function browses up one level
  46.          * according to the field: currentDirectory
  47.          */
  48.         private void upOneLevel(){
  49.                 if(this.currentDirectory.getParent()!= null)
  50.                         this.browseTo(this.currentDirectory.getParentFile());
  51.         }
  52.        
  53.         private void browseTo(finalFile aDirectory){
  54.                 if (aDirectory.isDirectory()){
  55.                         this.currentDirectory= aDirectory;
  56.                         fill(aDirectory.listFiles());
  57.                 }else{
  58.                         OnClickListener okButtonListener = new OnClickListener(){
  59.                                 // @Override
  60.                                 publicvoid onClick(DialogInterface arg0,int arg1){
  61.                                         try {
  62.                                                 // Lets start an intent to View the file, that was clicked...
  63.                                                 Intent myIntent = new Intent(android.content.Intent.VIEW_ACTION,
  64.                                                                 new ContentURI("file://"
  65.                                                                                 + aDirectory.getAbsolutePath()));
  66.                                                 startActivity(myIntent);
  67.                                         } catch (URISyntaxException e){
  68.                                                 e.printStackTrace();
  69.                                         }
  70.                                 }
  71.                         };
  72.                         OnClickListener cancelButtonListener = new OnClickListener(){
  73.                                 // @Override
  74.                                 publicvoid onClick(DialogInterface arg0,int arg1){
  75.                                         // Do nothing
  76.                                 }
  77.                         };
  78.                         AlertDialog.show(this,"Question","Do you want to open that file?n"
  79.                                                                 + aDirectory.getName(),
  80.                                                                 "OK", okButtonListener,
  81.                                                                 "Cancel", cancelButtonListener, false, null);
  82.                 }
  83.         }
  84.  
  85.         private void fill(File[] files){
  86.                 this.directoryEntries.clear();
  87.                
  88.                 // Add the "." and the ".." == 'Up one level'
  89.                 try {
  90.                         Thread.sleep(10);
  91.                 } catch (InterruptedException e1){
  92.                         // TODO Auto-generated catch block
  93.                         e1.printStackTrace();
  94.                 }
  95.                 this.directoryEntries.add(".");
  96.                
  97.                 if(this.currentDirectory.getParent()!= null)
  98.                         this.directoryEntries.add("..");
  99.                
  100.                 switch(this.displayMode){
  101.                         case ABSOLUTE:
  102.                                 for(File file : files){
  103.                                         this.directoryEntries.add(file.getPath());
  104.                                 }
  105.                                 break;
  106.                         case RELATIVE:// On relative Mode, we have to add the current-path to the beginning
  107.                                 int currentPathStringLenght= this.currentDirectory.getAbsolutePath().length();
  108.                                 for(File file : files){
  109.                                         this.directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLenght));
  110.                                 }
  111.                                 break;
  112.                 }
  113.                
  114.                 ArrayAdapter<String> directoryList= new ArrayAdapter<String>(this,
  115.                                 R.layout.file_row,this.directoryEntries);
  116.                
  117.                 this.setListAdapter(directoryList);
  118.         }
  119.  
  120.         @Override
  121.         protected void onListItemClick(ListView l,View v, int position, long id){
  122.                 int selectionRowID= (int)this.getSelectionRowID();
  123.                 String selectedFileString = this.directoryEntries.get(selectionRowID);
  124.                 if (selectedFileString.equals(".")){
  125.                         // Refresh
  126.                         this.browseTo(this.currentDirectory);
  127.                 } else if(selectedFileString.equals("..")){
  128.                         this.upOneLevel();
  129.                 } else {
  130.                         File clickedFile = null;
  131.                         switch(this.displayMode){
  132.                                 case RELATIVE:
  133.                                         clickedFile =new File(this.currentDirectory.getAbsolutePath()
  134.                                                                                                + this.directoryEntries.get(selectionRowID));
  135.                                         break;
  136.                                 case ABSOLUTE:
  137.                                         clickedFile =new File(this.directoryEntries.get(selectionRowID));
  138.                                         break;
  139.                         }
  140.                         if(clickedFile!= null)
  141.                                 this.browseTo(clickedFile);
  142.                 }
  143.         }
  144. }


原创粉丝点击