Android文件管理器只允许我深入一个文件夹

时间:2012-06-23 03:58:08

标签: android file-management

我已经创建了一个文件管理器,我将在其他程序中使用,但是我遇到了问题。它显示我的Android手机上的文件列表,我可以点击一个文件夹深,但不止一个它停止工作。主要是我认为因为“if(K.getAbsoluteFile()。isDirectory()== true)”,如果我摆脱这条线虽然它只是崩溃了。所以它似乎只允许我深入一个文件夹,任何人都可以弄清楚我在这里做错了什么?

此外,任何指南或有关于此主题的内容都会受到赞赏,这是我发现的随机代码的错误。


package book.BouncySquare;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FileManager extends ListActivity {
    public File clickedFile = null;
    private List<String> directoryEntries = new ArrayList<String>();

    //private List<String> directoryEntries = new ArrayList<String>();
    private File currentDirectory = new File("/");

    @Override
    public void onCreate(Bundle icicle) {
        Log.d("startx", "start");
        super.onCreate(icicle);
        browseToRoot();
    }



    public void browseToRoot() {
        Log.d("Browse", "browse"+currentDirectory.getAbsoluteFile());
        browseTo(new File("/"));
    }



    private void browseTo(File file) {
        Log.d("mew", "too");
        if(file.isDirectory()){
            Log.d("check", "it");
        fill(file);     }
    }


    private void browse(String x) {
        Log.d("created", "newfile");
        final File K=new File(x);   
        Log.d("broke", "ass");
        if(K.getAbsoluteFile().isDirectory()==true){
        Log.d("Z"+K.getAbsoluteFile(), "directory");
        fill(K.getAbsoluteFile());}
        else{
            Log.d("X"+K.getName(), "NotADirectoryOrHidden");
            Log.d("A"+K.getAbsoluteFile(), "directory");
        }

    }



    private void fill(File files) {
        File[] meow=null;
        this.directoryEntries.clear();
        meow= files.listFiles();
        Log.d("sss", "sss");
        this.directoryEntries.add(getString(R.string.current_dir));// directoryentries is an arraylist that holds our Directories names
        this.directoryEntries.add(getString(R.string.up_one_level));
        for (File file : meow) {
            this.directoryEntries.add(file.getName());      //fill our string array directoryentries with each files getName, then we pass it to arrayAdapter to populate
            }
        ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,R.layout.file_row, this.directoryEntries); //our context,layout, and array of directories is created
        this.setListAdapter(directoryList);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        String clickedEntry =this.directoryEntries.get((int) id);
        Log.d("clickedID="+id, "clickedPosition="+clickedEntry);    
        browse(clickedEntry);
    }
}

1 个答案:

答案 0 :(得分:1)

尝试将当前目录添加到您想要列出的内容中。

private void browse(String x) {
    Log.d("created", "newfile");
    final File K=new File(currentDirectory, x);   
    Log.d("broke", "ass");
    if(K.getAbsoluteFile().isDirectory()==true){
    Log.d("Z"+K.getAbsoluteFile(), "directory");
    currentDirectory = K.getAbsoluteFile();
    fill(K.getAbsoluteFile());}
    else{
        Log.d("X"+K.getName(), "NotADirectoryOrHidden");
        Log.d("A"+K.getAbsoluteFile(), "directory");
    }

}