如何检查是否有子文件夹中的Andr​​oid文件夹?

问题描述:

我想开发一款Android应用,可以列出所有的应用程序缓存大小和清理缓存,缓存的清洁工作正常,但要列出所有具有缓存是问题的应用程序。所以我想,检查应用程序包是否有缓存文件夹,如果它退出然后我试图检查缓存文件夹是否为空或没有,但我得到的力量关闭,我用的AsyncTask过,但得到力收,我检查我的code为普通的Java code,它的工作,不知道有什么问题,请指导我,先谢谢了。

I'm trying to develop an android app that could list all app's cache size and cleans the cache, cleaning of cache is working fine, but to list all the app which has cache is the problem. So I'm trying to check whether app package is having cache folder, if it exits then I'm trying to check whether cache folder is empty or not, but I'm getting force to close, I used AsyncTask too, but getting Force to close, I checked my code for plain java code, it's working, don't know what's the problem, please guide me, Thanks in Advance.

btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            new AsyncTask<Void, Void, Void>() 
            {
                @Override
                protected void onPreExecute() 
                {
                    pd = ProgressDialog.show(MainActivity.this, "Loading..",
                            "Please Wait", true, false);
                }//End of onPreExecute method

                @Override
                protected Void doInBackground(Void... params) 
                {
                    File file = new File("/data/data/com.android.browser/cache");


                    if(file.isDirectory()){

                        if(file.list().length>0){

                            System.out.println("Directory is not empty!");

                        }else{

                            System.out.println("Directory is empty!");

                        }

                    }else{

                        System.out.println("This is not a directory");

                    }

                    return null;
                }//End of doInBackground method

                @Override
                protected void onPostExecute(Void result) 
                {
                    pd.dismiss();

                }//End of onPostExecute method
            }.execute((Void[]) null);
        }
    });

有与AsyncTask的没有问题,我查了code。与其他的东西,它的罚款。请帮我解决这个谜。

There is no problem with AsyncTask I checked the code with other stuff, it's fine. Please help me in solving this riddle.

试试这个,

     .....
 List<File> files = getListFiles(new File("YOUR ROOT")); 
 ....
 private List<File> getListFiles(File parentDir) {
    ArrayList<File> inFiles = new ArrayList<File>();
    File[] files = parentDir.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            inFiles.addAll(getListFiles(file));
        } else {
            if(file.getName().endsWith(".csv")){
                inFiles.add(file);
            }
        }
    }
    return inFiles;
}