安卓:file.isDirectory()总是返回true的文件和放大器;所以无法添加过滤器 [英] Android : file.isDirectory() always returning true for a file & so unable to add filter

查看:226
本文介绍了安卓:file.isDirectory()总是返回true的文件和放大器;所以无法添加过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建Android中一个textpad这将加载任何txt文件,并显示该文件。我已经使用进口的java.io.File ,并试图按照code,但作为 dir.isDirectory()总是返回将其添加列表中的所有文件和我的过滤器不能正常工作。请帮助,如果有一个与code的任何问题,或者有什么建议可以添加过滤器,我的文件浏览器。

 公共类AndroidExplorer扩展ListActivity {

私人列表<字符串>项目= NULL;
私人列表<字符串> PATH = NULL;
私人字符串的root =/;
私人TextView的mypath中;


/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    mypath中=(TextView中)findViewById(R.id.path);
    GETDIR(根);
  }

私人无效GETDIR(字符串dirPath)
{
    的FilenameFilter过滤器=新的FilenameFilter(){

        @覆盖
        公共布尔接受(文件目录,字符串文件名){
            // TODO自动生成方法存根
            如果(dir.isDirectory())
            {
                的System.out.println(%%%%%%%%%%%+文件名);
               返回true;
            }
            其他
            {
                的System.out.println(*****+文件名);
                返回filename.endsWith(名为txt);
            }
        }
    };
    myPath.setText(位置:+ dirPath);

    项目=新的ArrayList<字符串>();
    路径=新的ArrayList<字符串>();
    文件F =新的文件(dirPath);
    文件[]文件= f.listFiles(过滤器);

    如果(!dirPath.equals(根))
    {

        item.add(根);
        path.add(根);

        item.add(../);
        path.add(f.getParent());

    }

    的for(int i = 0; I< files.length;我++)
    {
            文件fil​​e =文件[I]
            path.add(file.getPath());
            如果(file.isDirectory())
                item.add(file.getName()+/);
            其他
                item.add(file.getName());
    }

    ArrayAdapter<字符串>的fileList =
        新的ArrayAdapter<字符串>(这一点,R.layout.row,项目);
    setListAdapter(fileList)中的;
}

@覆盖
保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){

    档案文件=新的文件(path.get(位置));

    如果(file.isDirectory())
    {
        如果(file.canRead())
            GETDIR(path.get(位置));
        其他
        {
            新AlertDialog.Builder(本)
            .setIcon(R.drawable.icon)
            .setTitle([+ file.getName()+]文件夹无法读取!)
            .setPositiveButton(OK,
                    新DialogInterface.OnClickListener(){

                        @覆盖
                        公共无效的onClick(DialogInterface对话,诠释它){
                            // TODO自动生成方法存根
                        }
                    })。显示();
        }
    }
    其他
    {
        新AlertDialog.Builder(本)
            .setIcon(R.drawable.icon)
            .setTitle([+ file.getName()+])
            .setPositiveButton(OK,
                    新DialogInterface.OnClickListener(){

                        @覆盖
                        公共无效的onClick(DialogInterface对话,诠释它){
                            // TODO自动生成方法存根
                        }
                    })。显示();
    }
}
}
 

解决方案

您检查,如果该文件的目录(DIR)是过滤器的目录。当然,这将永远是正确的。您需要检查,如果文件本身是一个目录。你必须到文件名和目录转换成一个新的文件对象,并检查该文件重新presents目录。

我会建议使用的FileFilter而不是FilenameFitler。它已经有一个重新presents被过滤的文件的文件对象。

 的FileFilter过滤器=新的FileFilter(){

        @覆盖
        公共布尔接受(文件路径名){
             如果(pathname.isDirectory())
             {
                 的System.out.println(%%%%%%%%%%%+路径);
                返回true;
             }
             其他
             {
                 的System.out.println(*****+路径);
                 返回pathname.getName()的endsWith(名为txt)。
             }
        }
     };
 

I am trying to create a textpad in Android which will load any txt file and show the file. I have used import java.io.File and tried adding filter by following code but as dir.isDirectory() always returns true it add all the files in the list and my filter is not working. Please help if there is any problem with the code or any suggestions can add filter to my file explorer.

public class AndroidExplorer extends ListActivity {

private List<String> item = null;
private List<String> path = null;
private String root="/";
private TextView myPath;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    myPath = (TextView)findViewById(R.id.path);
    getDir(root);
  }

private void getDir(String dirPath)
{
    FilenameFilter filter = new FilenameFilter() {

        @Override
        public boolean accept(File dir, String filename) {
            // TODO Auto-generated method stub
            if(dir.isDirectory())
            {
                System.out.println("%%%%%%%%%%%"+filename);
               return true;
            }
            else
            {
                System.out.println("*****"+filename);
                return filename.endsWith(".txt");
            }
        }
    };
    myPath.setText("Location: " + dirPath);

    item = new ArrayList<String>();
    path = new ArrayList<String>();
    File f = new File(dirPath);
    File[] files = f.listFiles(filter);

    if(!dirPath.equals(root))
    {

        item.add(root);
        path.add(root);

        item.add("../");
        path.add(f.getParent());

    }

    for(int i=0; i < files.length; i++)
    {
            File file = files[i];
            path.add(file.getPath());
            if(file.isDirectory())
                item.add(file.getName() + "/");
            else
                item.add(file.getName());
    }

    ArrayAdapter<String> fileList =
        new ArrayAdapter<String>(this, R.layout.row, item);
    setListAdapter(fileList);
}

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

    File file = new File(path.get(position));

    if (file.isDirectory())
    {
        if(file.canRead())
            getDir(path.get(position));
        else
        {
            new AlertDialog.Builder(this)
            .setIcon(R.drawable.icon)
            .setTitle("[" + file.getName() + "] folder can't be read!")
            .setPositiveButton("OK", 
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                        }
                    }).show();
        }
    }
    else
    {
        new AlertDialog.Builder(this)
            .setIcon(R.drawable.icon)
            .setTitle("[" + file.getName() + "]")
            .setPositiveButton("OK", 
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                        }
                    }).show();
    }
}
}

解决方案

Your checking if the directory (dir) of the file is a directory in your filter. This will of course always be true. You need to check if the file itself is a directory. You'll have to convert the filename and directory into a new File object and check if that file represents a directory.

I'd recommend using a FileFilter instead of a FilenameFitler. It already has a file object that represents the file being filtered.

     FileFilter filter = new FileFilter() {

        @Override
        public boolean accept(File pathname) {
             if(pathname.isDirectory())
             {
                 System.out.println("%%%%%%%%%%%"+pathname);
                return true;
             }
             else
             {
                 System.out.println("*****"+pathname);
                 return pathname.getName().endsWith(".txt");
             }
        }
     };

这篇关于安卓:file.isDirectory()总是返回true的文件和放大器;所以无法添加过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆