【问题标题】:Cursor getContentResolver().query MediaStore is not fetching latest added Audio Files光标 getContentResolver().query MediaStore 未获取最新添加的音频文件
【发布时间】:2018-04-18 14:09:22
【问题描述】:

我正在使用 MediaStore 和 Cursor 从指定文件夹中获取音频文件。我想要的是,它应该从手机的内部存储中获取最新的歌曲列表,但它没有获取最新的文件。

例如:我正在录制音频,然后将其保存在特定文件夹中。但是当我在我的应用程序的播放列表中看到时。它不存在他们的。尽管该文件已成功保存在我的内部存储中。

我也调试过 cursor.getCount 也没有返回更新后的计数。

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);
}

【问题讨论】:

  • 它目前在做什么?好吧,它没有做你想做的事,但它在做什么呢?例子 ?输出?
  • @xoxel 我已经编辑了描述。请看一下。
  • 这显然不是我要的,示例?您是否尝试过通过打印名称、持续时间和 sourceLocation 进行调试?只是看看它是否正确?然后你有没有尝试在 cursor.close() 之后这样做?
  • 录制完成后是否使用MediaScannerConnection
  • @xoxel 看到光标计数返回 3,因为在该文件夹中存在大约 10 个文件。

标签: java android android-mediaplayer media-player mediastore


【解决方案1】:

这可以这样完成:

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();

【讨论】:

    【解决方案2】:

    所以最后,我想出了解决方案。我的音频文件没有出现在回收站视图中的原因是,当我录制音频并将其保存在我指定的路径中时,虽然它保存在那个位置但是 MediaStore.Audio.Media.EXTERNAL_CONTENT_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);
    

    添加后,我的音频文件也开始出现在我的回收站视图中。 像魅力一样工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多