内容解析器查询是不是SD卡上寻找音乐 [英] Content Resolver Query is not finding music on sd-card
问题描述
我已经建立了一个简单的查询,找上了Android模拟器的SD卡音乐。我使用Android的工作室。我放在使用亚行推SD卡3首歌曲。但是,查询未找到音乐文件。下面是文件在音乐文件夹中的SD卡图像:
下面是code,我使用执行查询:
ContentResolver的musicResolver = getContentResolver();
乌里musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
光标musicCursor = musicResolver.query(musicUri,NULL,NULL,NULL,NULL); //现在我们可以遍历结果,首先检查,我们有有效的数据:
如果(musicCursor =空&放大器;!&放大器; musicCursor.moveToFirst()){ //我们首先检索,我们感兴趣的是为每首歌曲的数据项目的列索引
INT titleColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.TITLE);
INT idColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media._ID);
INT artistColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.ARTIST); //然后我们用这些来创建一个新的歌曲对象,并将其添加到列表
做{
长thisId = musicCursor.getLong(idColumn);
字符串thisTitle = musicCursor.getString(titleColumn);
字符串thisArtist = musicCursor.getString(artistColumn);
allSongsPlaylist.add(新曲(thisId,thisTitle,thisArtist));
}
而(musicCursor.moveToNext());
}
musicCursor不为空,但musicCursor.moveToNext()返回0。
我是什么做错了吗?音乐文件显然有对SD卡,但查询看不到这些。如果我更改内容URI来INTERNAL_CONTENT_URI,查询在内部存储查找文件(例如压电报警器),但不是我的音乐文件。
我是什么做错了吗?
块引用>如果要我猜,你是假设
MediaStore
知道这些文件。
MediaStore
,对自己,只索引外部存储偶然。它没有,你通过推送的文件有知识的亚行
有关轻量级的测试,抓住自己从触发一轮编制索引Play商店的应用程序。我用这个应用,但显然它不支持Android 4.4或以上版本。有很多人,像这个,那可能会为你工作。或者,等了一天,并尝试你的测试,明天又来了,他们可能会通过,然后自动拾取。
开发的文件写入外部存储编程应该使用
MediaScannerConnection
和SCANFILE()
来获取这些文件编入索引MediaStore
。I have constructed a simple query to find music on the sdcard of the Android emulator. I am using Android Studio. I placed 3 songs on the sdcard using adb push. However, the query is not finding the music files. Here is an image of the files on the sdcard in the Music folder:
Here is the code that I am using to perform the query:
ContentResolver musicResolver = getContentResolver(); Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null); // Now we can iterate over the results, first checking that we have valid data: if(musicCursor!=null && musicCursor.moveToFirst()){ // We first retrieve the column indexes for the data items that we are interested in for each song int titleColumn = musicCursor.getColumnIndex (android.provider.MediaStore.Audio.Media.TITLE); int idColumn = musicCursor.getColumnIndex (android.provider.MediaStore.Audio.Media._ID); int artistColumn = musicCursor.getColumnIndex (android.provider.MediaStore.Audio.Media.ARTIST); // then we use these to create a new Song object and add it to the list do { long thisId = musicCursor.getLong(idColumn); String thisTitle = musicCursor.getString(titleColumn); String thisArtist = musicCursor.getString(artistColumn); allSongsPlaylist.add(new Song(thisId, thisTitle, thisArtist)); } while (musicCursor.moveToNext()); }
musicCursor is not null, but musicCursor.moveToNext() returns 0.
What am I doing wrong? The music files are obviously there on the sdcard, but the query doesn't see them. If I change the content URI to INTERNAL_CONTENT_URI, the query is finding files in internal storage (e.g. piezo alarm), but not my music files.
解决方案What am I doing wrong?
If I had to guess, you are assuming that
MediaStore
knows about those files.
MediaStore
, on its own, only indexes external storage occasionally. It has no knowledge of files that you push there viaadb
.For lightweight testing, grab yourself an app from the Play Store that triggers a reindexing round. I used to use this app, but apparently it does not support Android 4.4+. There are plenty of others, like this one, that may work for you. Or, wait a day and try your tests again tomorrow, and they may get picked up automatically by then.
Developers that write files to external storage programmatically should use
MediaScannerConnection
andscanFile()
to get those files indexed byMediaStore
.这篇关于内容解析器查询是不是SD卡上寻找音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!