Java 游标getContentResolver()。查询MediaStore未获取最新添加的音频文件
我正在使用MediaStore和游标从指定文件夹获取音频文件。我想要的是,它应该从手机的内部存储器中获取最新的歌曲列表,但它不会获取最新的文件 例如:我正在录制音频,然后将其保存在特定文件夹中。但当我在应用程序的播放列表中看到时。这不是他们的礼物。尽管文件已成功保存在我的内部存储器中 我也对它进行了调试cursor.getCount也没有返回更新的计数数Java 游标getContentResolver()。查询MediaStore未获取最新添加的音频文件,java,android,android-mediaplayer,media-player,mediastore,Java,Android,Android Mediaplayer,Media Player,Mediastore,我正在使用MediaStore和游标从指定文件夹获取音频文件。我想要的是,它应该从手机的内部存储器中获取最新的歌曲列表,但它不会获取最新的文件 例如:我正在录制音频,然后将其保存在特定文件夹中。但当我在应用程序的播放列表中看到时。这不是他们的礼物。尽管文件已成功保存在我的内部存储器中 我也对它进行了调试cursor.getCount也没有返回更新的计数数 String AudioFilePath = "%" +"/nexcox/voicerecorder/audio/" +"%"; Uri u
String AudioFilePath = "%" +"/nexcox/voicerecorder/audio/" +"%";
Uri uri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection= MediaStore.Audio.Media.DATA +" LIKE ? ";
Cursor cursor=context.getContentResolver().query(uri,null,selection,
new String[]{AudioFilePath},
null);
if (cursor!=null){
if (cursor.moveToFirst()){
do {
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
String sourceLocation = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
AudioInfo audioInfo=new AudioInfo(name,duration,sourceLocation);
audioInfos.add(audioInfo);
}while (cursor.moveToNext());
}
cursor.close();
audioAdapter=new AudioAdapter(context,audioInfos);
recyclerView.setAdapter(audioAdapter);
}
可以这样做:
String[] projection = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DISPLAY_NAME};
Cursor myCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection , null, null, null);
if(myCursor != null){
if(myCursor .moveToFirst()){
do{
int audioIndex = myCursor .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
audioList.add(myCursor .getString(audioIndex));
}while(myCursor .moveToNext());
}
}
myCursor .close();
最后,我找到了解决办法。我的音频文件没有显示在“回收器”视图中的原因是,当我录制音频并将其保存在指定路径中时,虽然它保存在该位置,但MediaStore.audio.Media.EXTERNAL\u CONTENT\u URI不知道我刚才保存的文件是音频媒体。因此,为了告诉MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,在停止mediaRecorder时添加了以下代码
ContentValues values = new ContentValues(4);
long current = System.currentTimeMillis();
values.put(MediaStore.Audio.Media.TITLE, "audio file");
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
values.put(MediaStore.Audio.Media.DATA, AudioFilePath);
ContentResolver contentResolver = context.getContentResolver();
// Construct uris
Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);
添加之后,我的音频文件也开始显示在我的recyclerview中。
就像一个符咒。它目前在做什么?好吧,它没有做你想做的,但是它在做什么呢?例子?输出?@xoxel我已编辑描述。请看一看。这显然不是我想要的,举例来说?您是否尝试过通过打印名称、持续时间和源位置进行调试?只是想看看它是否正确?然后您是否尝试在游标.close()之后执行此操作?录制完成后是否使用
MediaScannerConnection
。@xoxel请查看游标计数返回3,其中大约有10个文件存在于该文件夹中。