如何使用android.database.Cursor获取所有音频文件的列表?

时间:2017-11-11 09:54:36

标签: java android android-contentprovider android-cursor android-cursorloader

我通过the solution

发现了Cursor此问题
public class MainActivity extends AppCompatActivity {

    void example() {

        List<File> localAudioFiles = new LinkedList<>();

        Uri contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String[] projection = {MediaStore.Images.Media.DATA};

        // The problem is that it contains a list of files as of the moment
        // when the operating system has been rebooted.
        try (Cursor cursor = getContentResolver().query(
                contentUri,
                projection,
                MediaStore.Audio.Media.IS_MUSIC + " != 0",
                null,
                null);) {

            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
            while (cursor.moveToNext()) {
                String audio = cursor.getString(column_index);    
                localAudioFiles.add(new File(audio));
            }

        } catch (RuntimeException ex) {
        }
    }
}

但据我了解,其状态需要强制更新,因为在重启设备之前,不会以这种方式检测通过AVD文件管理器添加的文件。

  • 如何强制更新实体Cursor的状态?
  • 解决方案是值得的,还是不是比递归遍历所有目录更好?

0 个答案:

没有答案