简体   繁体   中英

Cursor getContentResolver().query MediaStore is not fetching latest added Audio Files

I am using MediaStore and Cursor to fetch audio files from a specified folder. What I want is that, it should fetch the latest list of songs from the phone's internal storage but it is not fetching latest files.

For Example: I am recording audio, and then it is saved in specific folder. but when I see in the playlist of my app. It is not present their. Although the file is successfully saved in my internal storage.

I have debugged it as well cursor.getCount is not returning the updated number of count either.

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

在此处输入图片说明

This could been done like this :

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

So finally, I have figured out the solution. the reason why my audio files were not showing up in the recycler view was that , when I was recording the audio and saving it in my specified path, although it got saved in that location but MediaStore.Audio.Media.EXTERNAL_CONTENT_URI did not know that the file i just saved is an Audio Media. So in order to tell this to MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, The following code was added while stopping the 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);

After adding that my audio file started to show up in my recyclerview as well. works like a charm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM