简体   繁体   English

如何加快文件存储访问?

[英]How to speed up File Storage Access?

I am trying to get a list of all Folders that contain MP3 Files on the user's Internal Storage. 我正在尝试获取用户内部存储器上包含MP3文件的所有文件夹的列表。

Here is the recursive function that I am calling for this purpose - 这是我为此目的调用的递归函数-

public void listAllMusicFiles(String pathToDirectory) {
        String lastFolderPath = "";
        int mp3Count = 0;
        File f = new File(pathToDirectory);
        File[] files = f.listFiles();
        for (File inFile : files) {
            if (inFile.isDirectory()) {
                //reset last folder path
                lastFolderPath = "";
                Log.d("Directory ", inFile.getPath());
                listAllMusicFiles(inFile.getPath());
            } else {
                if (inFile.getAbsolutePath().endsWith(".mp3") || inFile.getAbsolutePath().endsWith(".MP3")) {
                    mp3Count++;
                    Log.wtf("MP3 Count", mp3Count + " ");

                    //add each folder only once
                    String folderName = inFile.getParentFile().getName();
                    String folderPath = inFile.getParentFile().getPath();
                    Log.e("FOUND in", folderPath);

                    //create a new Folder object
                    Folder currentFolder = new Folder(folderName, folderPath, mp3Count + "");

                    if (!lastFolderPath.equals(folderPath)) {
                        Log.d("NEW", folderPath);
                        lastFolderPath = folderPath;
                        folderArrayList.add(currentFolder);
                    } else {
                        Log.d("OLD", folderPath);
                        //find a Folder object in folderArrayList where the object's path matches current folderPath
                        for (Folder folder : folderArrayList) {
                            String currentPath = folder.getFolder_Path();
                            if (currentPath.equals(folderPath)) {
                                //found a match
                                //update count
                                folder.setFolder_Song_Count(mp3Count + "");
                            }
                        }
                    }
                }
            }
        }
    }

When I run this code on my device, I am able to list the required folders in a RecyclerView, but with a delay of about 6-7 seconds. 当我在设备上运行此代码时,我可以在RecyclerView中列出所需的文件夹,但会延迟大约6-7秒。

I have already moved this task into an AsyncTask, so that my UIThread does not hang because of this intensive operation. 我已经将此任务移至AsyncTask中,因此由于执行此操作过多,UIThread不会挂起。

But I am totally at a loss when it comes to improving File System Performance. 但是,在提高文件系统性能方面我完全茫然。 Kindly help. 请帮助。 Thanks ! 谢谢 !

Instead of storing currentFolder in an ArrayList and in the next step iterating through complete list to find that folder and updating the value, you can simply use HashMap like this 无需将currentFolder存储在ArrayList中,而在下一步中遍历完整列表以查找该文件夹并更新值,您可以像这样简单地使用HashMap

HashMap<String, Folder> folders = new HashMap<>();

    public void listAllMusicFiles(String pathToDirectory) {

        int mp3Count = 0;
        File f = new File(pathToDirectory);
        File[] files = f.listFiles();

        Folder folder;
        String folderName, folderPath;

        for (File inFile : files) {
            if (inFile.isDirectory()) {
                //reset last folder path
                Log.d("Directory ", inFile.getPath());
                listAllMusicFiles(inFile.getPath());
            } else {
                if (inFile.getAbsolutePath().endsWith(".mp3") || inFile.getAbsolutePath().endsWith(".MP3")) {
                    mp3Count++;
                    Log.wtf("MP3 Count", mp3Count + " ");

                    //add each folder only once
                    folderName = inFile.getParentFile().getName();
                    folderPath = inFile.getParentFile().getPath();

                    Log.e("FOUND in", folderPath);

                    if (folders.containsKey(folderPath)) {

                        folder = folders.get(folderPath);
                        folder.setFolder_Song_Count(mp3Count + "");
                        folders.put(folderPath, folder);
                    } else {

                        folder = new Folder(folderName, folderPath, mp3Count + "");
                        folders.put(folderPath, folder);
                    }

                }
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM