Android ContentResolver.query always returns the same data

I have a video recording app with a file list that lists all the videos on the SD card.

Code inspired by I want to get audio files on SD card

Using ContentResolver works as expected, but does not update if the files in the map change. I don't mean automatically, but after viewing / reloading the app. Even reinstalling the app didn't help, it still shows the same files. The deleted video file is not displayed via the PC and cannot be played (this video cannot be played (translated)).

I dumped data and the problem is not caching or elsewhere. I am not doing any caching myself and have not found anything in this question. Thanks you

code:

    // acquisition  
    String[] projection = {
        MediaStore.Video.Media._ID,
        MediaStore.Video.Media.DISPLAY_NAME,
        MediaStore.Video.Media.DURATION,
        MediaStore.Video.Media.DATA
    };

    ContentResolver resolver = getActivity().getContentResolver();
    Cursor videoCursor = resolver.query(
        MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
        projection,
        null,
        null,
        null
    );

    // extraction
    while(cursor.moveToNext()) {
        cursorIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        filepath = cursor.getString(cursorIndex);

        cursorIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
        filename = cursor.getString(cursorIndex);

        cursorIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
        duration = cursor.getString(cursorIndex);

        result[ index++ ] = new VideoFileMetadata(filename, duration, filepath);
    }

      

Edit 1 [14-03-2013]:

I tried adding number + " = " + number

an ORDER or WHERE clause to act as a potential caching query cache, but it had no effect (although it may have been removed by the optimizer as a useless suggestion). This time, I reinstalled the application from a different computer using a different certificate, but the result of the request remained the same, pointing to the currently nonexistent files.

0


source to share


1 answer


You must call first cursor.moveToFirst()

.

So your cursor iteration loop should look like



if (cursor.moveToFirst()) {
    do {
        // cursorIndex = cursor.getColumnIndexOrThrow, etc...
    } while (cursor.moveToNext());
}

      

+4


source







All Articles