java - Android应用程序: scanning for music files in specified folders runs slowly and hangs.

标签 java android

我正在尝试扫描指定文件夹中的所有音乐文件,但遇到性能问题且应用程序关闭。

我的方法是首先使用以下方法从手机获取所有音乐文件:

String[] projection = { 
        MediaStore.Audio.Media._ID,
        MediaStore.Audio.Media.DISPLAY_NAME,
        MediaStore.Audio.Media.DATA, 
        MediaStore.Audio.Media.TITLE 
};

String selection = MediaStore.Audio.Media.IS_MUSIC + "!=" + 0;

cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                      projection, 
                      selection, 
                      null, 
                      MediaStore.Audio.Media.TITLE + " ASC");

然后我使用以下方法获取每个获取的文件的位置:

columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);

如果文件位置位于指定文件夹中,我会将其添加到将在屏幕上显示的列表中。

这种方法的问题在于它的性能不佳。

它挂起 4-5 秒。

如果我滚动列表,应用程序就会关闭。

下面是我现在使用的完整代码:

private String[] getTrackList() {
    String[] result = null;
    String[] projection = {
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.DISPLAY_NAME,
            MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.TITLE
    };

    String selection = MediaStore.Audio.Media.IS_MUSIC + "!=" + 0;

    cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            projection,
            selection,
            null,
            MediaStore.Audio.Media.TITLE + " ASC");

    result = new String[cursor.getCount()];
    int position = 0;

    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        getSearchableDir("/mnt/sdcard/Music/"); //See below
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        String currentDir = cursor.getString(columnIndex).substring(0,
                cursor.getString(columnIndex).lastIndexOf("/") + 1);

        if (searchableDir.contains(currentDir)) {
            columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
            result[position] = cursor.getString(columnIndex);
            position++;
        }
    }
    return result;
}

// Function for recursively fetching all the sub-directories
private void getSearchableDir(String path) {
    searchableDir.add(path);
    File root = new File(path);
    File[] list = root.listFiles();
    for (File f : list) {
        if (f.isDirectory()) {
            searchableDir.add(f.getAbsolutePath());
            getSearchableDir(f.getAbsolutePath());
        }
    }
}

最佳答案

我认为可以肯定地说 getSearchableDir 是这里的性能消耗者。

重构代码,以便不调用此函数。

关于java - Android应用程序: scanning for music files in specified folders runs slowly and hangs.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11291860/

相关文章:

java - 获取第一个除数超过 400 的三角形数

java - 数组改组不起作用

java - java.nio.file 在哪里

java - 将接口(interface)与其实现分离有什么意义?

java - 如何在 Spring Hibernate 5 LocalSessionFactoryBean 中注入(inject) Spring 管理的 CacheRegionFactory

android - 如何在 AlertDialog 中为 EditText 设置边距

javascript - 使用 javascript 接口(interface)重新加载 Android webview

android - 如何断开 HTC(Froyo 及以下)手机上的 HttpUrlConnection?

android - Razorpay 在支付页面崩溃,在 flutter 中找不到类 "com.razorpay.d__1_"

android - 如何在我的应用程序中嵌入网站