1

我有一个简单的文件管理器。在我浏览文件的目录中,它只会显示一些特定的文件扩展名,例如仅 .txt 和 .pdf 。它还将显示目录。我该怎么做。以下代码应该添加或修改什么。FileManager.java 文件是-

package com.radiobot.speedreaderv1_1;


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.content.Intent;
import android.content.DialogInterface.OnClickListener;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FileManager extends ListActivity {

        private enum DISPLAYMODE{ ABSOLUTE, RELATIVE; }

        private final DISPLAYMODE displayMode = DISPLAYMODE.ABSOLUTE;
        private List<String> directoryEntries = new ArrayList<String>();
        private File currentDirectory = new File("/");

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle icicle) {
                super.onCreate(icicle);
                browseToRoot();
        }


        private void browseToRoot() {
                browseTo(new File("/"));
    }


        private void upOneLevel(){
                if(this.currentDirectory.getParent() != null)
                        this.browseTo(this.currentDirectory.getParentFile());
        }

        private void browseTo(final File aDirectory){
                if (aDirectory.isDirectory()){
                        this.currentDirectory = aDirectory;
                        fill(aDirectory.listFiles());
                }
                    else{
                        OnClickListener okButtonListener = new OnClickListener(){
                                // @Override
                                public void onClick(DialogInterface arg0, int arg1) {
                                        {


                                            try
                                            {
                                            Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);

                                            File file = new File(aDirectory.getAbsolutePath());
                                            String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());

                                            String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
                                            myIntent.setDataAndType(Uri.fromFile(file),mimetype);
                                            startActivity(myIntent);
                                            }
                                            catch (Exception e)
                                            {
                                            e.getMessage();
                                            }

                                        }
                                }
                        };
                        OnClickListener cancelButtonListener = new OnClickListener(){
                                // @Override
                                public void onClick(DialogInterface arg0, int arg1) {
                                        // Do nothing
                                }
                        };

                        new AlertDialog.Builder(this)
                        .setIcon(R.drawable.ic_launcher)
                        .setTitle(
                                "Do you want to open this file" + aDirectory.getName())
                        .setPositiveButton("OK", okButtonListener)
                        .setNegativeButton("Cancel", cancelButtonListener)
                                                                .show();
                }
        }

        private void fill(File[] files) {
                this.directoryEntries.clear();

                try {
                        Thread.sleep(10);
                } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                }
                this.directoryEntries.add("");

                if(this.currentDirectory.getParent() != null)
                        this.directoryEntries.add("..");

                switch(this.displayMode){
                        case ABSOLUTE:
                                for (File file : files){
                                        this.directoryEntries.add(file.getPath());
                                }
                                break;
                        case RELATIVE:
                                int currentPathStringLenght = this.currentDirectory.getAbsolutePath().length();
                                for (File file : files){
                                        this.directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLenght));
                                }
                                break;
                }

                ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,
                                R.layout.filemanager, this.directoryEntries);

                this.setListAdapter(directoryList);
        }

        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {

            String position2 = (String) l.getItemAtPosition(position);

        String selectedFileString = position2;

                if (selectedFileString.equals(".")) {
                        // Refresh
                        this.browseTo(this.currentDirectory);
                } else if(selectedFileString.equals("..")){
                        this.upOneLevel();
                } else {
                        File clickedFile = null;
                        switch(this.displayMode){
                                case RELATIVE:

                                    clickedFile = new File(position2);
                                        break;
                                case ABSOLUTE:

                                    clickedFile = new File(position2);    
                                    break;
                        }
                        if(clickedFile != null)

                                this.browseTo(clickedFile);
                }
        }


}
4

2 回答 2

0

这可能很有用。

FileFilter filter = new FileNameExtensionFilter("JPEG 文件", "jpg", "jpeg"); JFileChooser 文件选择器 = ...; fileChooser.addChoosableFileFilter(过滤器);

于 2013-06-18T16:24:42.870 回答
0

browseTo()您将目录中的所有文件添加到列表中:

fill(aDirectory.listFiles()); // here's the problem

看一下File.listFiles(FileFilter filter),这只会返回符合指定过滤器的文件。

File[] files = aDirectory.listFiles(new FileFilter() {
    @Override
    boolean accept(File pathname) {
        // return true if and only if pathname has the desired extension
        if (pathname.getName().endsWith(".txt") || pathname.isDirectory())
            return true; // accept only .txt files and directories
        else
            return false;
    }
});
于 2013-06-18T16:44:40.997 回答