Android之简单文件管理器


这里运用Java I/O、ListActivity、Dialog、Bitmap等实现简单文件管理器,可以查看目录文件,修改文件名,删除文件,打开文件。比较简单,直接看代码:

先看布局文件:

layout/main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <ListView    
  8.     android:id="@android:id/list"  
  9.     android:layout_width="wrap_content"  
  10.     android:layout_height="wrap_content"  
  11.     />  
  12. </LinearLayout>  
文件列表布局:

layout/file.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <ImageView  
  8.     android:id="@+id/imageView"  
  9.     android:layout_width="wrap_content"  
  10.     android:layout_height="wrap_content"  
  11. />  
  12. <TextView  
  13.   android:id="@+id/textView"  
  14.   android:layout_width="wrap_content"  
  15.   android:layout_height="wrap_content"  
  16.   android:textSize="14sp">  
  17. </TextView>  
  18. </LinearLayout>  

修改文件名对话框布局文件:

layout/rename_dialog.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="match_parent"  
  5.   android:layout_height="match_parent">  
  6.   <EditText  
  7.         android:id="@+id/editText"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"    
  10.   />  
  11. </LinearLayout>  
主Activity:
  1. public class MainActivity extends ListActivity {  
  2.     private static final String ROOT_PATH = "/";  
  3.     //存储文件名称   
  4.     private ArrayList<String> names = null;  
  5.     //存储文件路径   
  6.     private ArrayList<String> paths = null;  
  7.     private View view;  
  8.     private EditText editText;  
  9.     /** Called when the activity is first created. */  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.main);  
  14.         //显示文件列表   
  15.         showFileDir(ROOT_PATH);  
  16.     }  
  17.     private void showFileDir(String path){  
  18.         names = new ArrayList<String>();  
  19.         paths = new ArrayList<String>();  
  20.         File file = new File(path);  
  21.         File[] files = file.listFiles();  
  22.           
  23.         //如果当前目录不是根目录   
  24.         if (!ROOT_PATH.equals(path)){  
  25.             names.add("@1");  
  26.             paths.add(ROOT_PATH);  
  27.               
  28.             names.add("@2");  
  29.             paths.add(file.getParent());  
  30.         }  
  31.         //添加所有文件   
  32.         for (File f : files){  
  33.             names.add(f.getName());  
  34.             paths.add(f.getPath());  
  35.         }  
  36.         this.setListAdapter(new MyAdapter(this,names, paths));  
  37.     }  
  38.     @Override  
  39.     protected void onListItemClick(ListView l, View v, int position, long id) {  
  40.         String path = paths.get(position);  
  41.         File file = new File(path);  
  42.         // 文件存在并可读   
  43.         if (file.exists() && file.canRead()){  
  44.             if (file.isDirectory()){  
  45.                 //显示子目录及文件   
  46.                 showFileDir(path);  
  47.             }  
  48.             else{  
  49.                 //处理文件   
  50.                 fileHandle(file);  
  51.             }  
  52.         }  
  53.         //没有权限   
  54.         else{  
  55.             Resources res = getResources();  
  56.             new AlertDialog.Builder(this).setTitle("Message")  
  57.             .setMessage(res.getString(R.string.no_permission))  
  58.             .setPositiveButton("OK",new OnClickListener() {  
  59.                 @Override  
  60.                 public void onClick(DialogInterface dialog, int which) {  
  61.                       
  62.                 }  
  63.             }).show();  
  64.         }  
  65.         super.onListItemClick(l, v, position, id);  
  66.     }  
  67.     //对文件进行增删改   
  68.     private void fileHandle(final File file){  
  69.         OnClickListener listener = new DialogInterface.OnClickListener() {  
  70.             @Override  
  71.             public void onClick(DialogInterface dialog, int which) {  
  72.                 // 打开文件   
  73.                 if (which == 0){  
  74.                     openFile(file);  
  75.                 }  
  76.                 //修改文件名   
  77.                 else if(which == 1){  
  78.                     LayoutInflater factory = LayoutInflater.from(MainActivity.this);  
  79.                     view = factory.inflate(R.layout.rename_dialog, null);  
  80.                     editText = (EditText)view.findViewById(R.id.editText);  
  81.                     editText.setText(file.getName());  
  82.                       
  83.                     OnClickListener listener2 = new DialogInterface.OnClickListener() {  
  84.                         @Override  
  85.                         public void onClick(DialogInterface dialog, int which) {  
  86.                             // TODO Auto-generated method stub   
  87.                             String modifyName = editText.getText().toString();  
  88.                             final String fpath = file.getParentFile().getPath();  
  89.                             final File newFile = new File(fpath + "/" + modifyName);  
  90.                             if (newFile.exists()){  
  91.                                 //排除没有修改情况   
  92.                                 if (!modifyName.equals(file.getName())){  
  93.                                     new AlertDialog.Builder(MainActivity.this)  
  94.                                     .setTitle("注意!")  
  95.                                     .setMessage("文件名已存在,是否覆盖?")  
  96.                                     .setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  97.                                         @Override  
  98.                                         public void onClick(DialogInterface dialog, int which) {  
  99.                                             if (file.renameTo(newFile)){  
  100.                                                 showFileDir(fpath);  
  101.                                                 displayToast("重命名成功!");  
  102.                                             }  
  103.                                             else{  
  104.                                                 displayToast("重命名失败!");  
  105.                                             }  
  106.                                         }  
  107.                                     })  
  108.                                     .setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  109.                                         @Override  
  110.                                         public void onClick(DialogInterface dialog, int which) {  
  111.                                               
  112.                                         }  
  113.                                     })  
  114.                                     .show();  
  115.                                 }  
  116.                             }  
  117.                             else{  
  118.                                 if (file.renameTo(newFile)){  
  119.                                     showFileDir(fpath);  
  120.                                     displayToast("重命名成功!");  
  121.                                 }  
  122.                                 else{  
  123.                                     displayToast("重命名失败!");  
  124.                                 }  
  125.                             }  
  126.                         }  
  127.                     };  
  128.                     AlertDialog renameDialog = new AlertDialog.Builder(MainActivity.this).create();  
  129.                     renameDialog.setView(view);  
  130.                     renameDialog.setButton("确定", listener2);  
  131.                     renameDialog.setButton2("取消"new DialogInterface.OnClickListener() {  
  132.                         @Override  
  133.                         public void onClick(DialogInterface dialog, int which) {  
  134.                             // TODO Auto-generated method stub   
  135.                               
  136.                         }  
  137.                     });  
  138.                     renameDialog.show();  
  139.                 }  
  140.                 //删除文件   
  141.                 else{  
  142.                     new AlertDialog.Builder(MainActivity.this)  
  143.                     .setTitle("注意!")  
  144.                     .setMessage("确定要删除此文件吗?")  
  145.                     .setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  146.                         @Override  
  147.                         public void onClick(DialogInterface dialog, int which) {  
  148.                             if(file.delete()){  
  149.                                 //更新文件列表   
  150.                                 showFileDir(file.getParent());  
  151.                                 displayToast("删除成功!");  
  152.                             }  
  153.                             else{  
  154.                                 displayToast("删除失败!");  
  155.                             }  
  156.                         }  
  157.                     })  
  158.                     .setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  159.                         @Override  
  160.                         public void onClick(DialogInterface dialog, int which) {  
  161.                               
  162.                         }  
  163.                     }).show();  
  164.                 }  
  165.             }  
  166.         };  
  167.         //选择文件时,弹出增删该操作选项对话框   
  168.         String[] menu = {"打开文件","重命名","删除文件"};  
  169.         new AlertDialog.Builder(MainActivity.this)  
  170.         .setTitle("请选择要进行的操作!")  
  171.         .setItems(menu, listener)  
  172.         .setPositiveButton("取消"new DialogInterface.OnClickListener() {  
  173.             @Override  
  174.             public void onClick(DialogInterface dialog, int which) {  
  175.                   
  176.             }  
  177.         }).show();  
  178.     }  
  179.     //打开文件   
  180.     private void openFile(File file){  
  181.         Intent intent = new Intent();  
  182.         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  183.         intent.setAction(android.content.Intent.ACTION_VIEW);  
  184.           
  185.         String type = getMIMEType(file);  
  186.         intent.setDataAndType(Uri.fromFile(file), type);  
  187.         startActivity(intent);  
  188.     }  
  189.     //获取文件mimetype   
  190.     private String getMIMEType(File file){  
  191.         String type = "";  
  192.         String name = file.getName();  
  193.         //文件扩展名   
  194.         String end = name.substring(name.lastIndexOf(".") + 1, name.length()).toLowerCase();  
  195.         if (end.equals("m4a") || end.equals("mp3") || end.equals("wav")){  
  196.             type = "audio";  
  197.         }  
  198.         else if(end.equals("mp4") || end.equals("3gp")) {  
  199.             type = "video";  
  200.         }  
  201.         else if (end.equals("jpg") || end.equals("png") || end.equals("jpeg") || end.equals("bmp") || end.equals("gif")){  
  202.             type = "image";  
  203.         }  
  204.         else {  
  205.             //如果无法直接打开,跳出列表由用户选择   
  206.             type = "*";  
  207.         }  
  208.         type += "/*";  
  209.         return type;  
  210.     }  
  211.     private void displayToast(String message){  
  212.         Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();  
  213.     }  
  214. }  
  • 1
  • 2
  • 下一页

相关内容