Android查找SD卡上的文件

发布于 2024-10-06 10:54:51 字数 211 浏览 0 评论 0原文

我正在开发一个 MP3 应用程序,我想在其中索引 SDCard 上的文件。最好的方法是什么?

我的想法。当应用程序第一次启动时搜索文件,并为 SDCard 状态更改意图注册一个广播接收器,并在广播该意图时搜索文件。

但在这种情况下,如果我的广播接收器在 10 秒内没有返回,ANR 就会出现。

寻找更好、更安全的想法。谢谢。

I'm working on a MP3 application in which I'd like to index the files on my SDCard. What is the best way to do it?

My Idea. Search for files when the application is started for the first time and register a broadcast receiver for the SDCard state change intents and search for the files whenever the intent is broadcasted.

But in this case the ANR would show up if my broadcast receiver doesn't return within 10 seconds.

Looking for better and failsafe ideas. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离去的眼神 2024-10-13 10:54:51

同意 Chris 的观点,MediaScanner 会为您查找音乐,填充 MediaStore 数据库。下面是一些查找音乐条目的代码:

        final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        final String[] cursor_cols = {
                MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.ALBUM,
                MediaStore.Audio.Media.TITLE,
        };
        final String where = MediaStore.Audio.Media.IS_MUSIC + "=1";
        final Cursor cursor = getContentResolver().query(uri, cursor_cols, where, null, null);
        cursor.moveToNext();
        final String artist = cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
        final String album = cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
        final String track = cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
        doSomethingInteresting(artist, album, track);

“数据”字段包含一个可与 MediaPlayer 一起使用的句柄。

Agree with Chris, MediaScanner finds music for you, populating the MediaStore database. Here's some code to look up a music entry:

        final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        final String[] cursor_cols = {
                MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.ALBUM,
                MediaStore.Audio.Media.TITLE,
        };
        final String where = MediaStore.Audio.Media.IS_MUSIC + "=1";
        final Cursor cursor = getContentResolver().query(uri, cursor_cols, where, null, null);
        cursor.moveToNext();
        final String artist = cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
        final String album = cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
        final String track = cursor.getString(_cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
        doSomethingInteresting(artist, album, track);

The "data" field contains a handle you can use with MediaPlayer.

眼趣 2024-10-13 10:54:51

为了避免 ANR,您需要在 UI 线程之外进行搜索。由于 SD 卡可能很大,您可能希望在服务中而不是在前台活动中执行此操作,以便用户可以在搜索正在进行时使用其设备执行其他操作。

但是 android 已经找到并索引了受支持的媒体文件,因此您应该看看是否可以利用内置的 MediaScanner 功能。

To avoid the ANR you need to do the search outside the UI thread. As SD cards can be big, you probably want to do it in a service rather than in your foreground activity so that the user can use their device for other things while the search is ongoing.

But android already finds and indexes supported media files, so you should see if you can leverage the built-in MediaScanner stuff.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文