Android ContentResolver.query always returns same data

∥☆過路亽.° 提交于 2019-12-25 02:53:14

问题


I have videoplayer app with filebrowser listing all videos on SD card

Code inspired by i want get audio files in sd card

Using ContentResolver, works as expected, but it does not update if the files on card change. I do not mean automatically, but after view/app restart. Not even reinstalling the application helped, still shows the same files. The deleted video file is not visible via PC nor it is possible to play it (This video cannot be played (translation)).

I dumped the data and the problem is not in view caching or elsewhere. I do not implement any caching of my own and failed to find anything on the matter. Thank 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 to ORDER or WHERE clause to act as a potential query caching buster, but it had no effect (although it's possible it was removed by an optimizer as a useless clause). This time I had reinstalled the application from a different machine using different certificate, but the query result remained the same, listing currently non-existing files.


回答1:


You should first call cursor.moveToFirst() .

So, your cursor iteration loop should look like

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


来源:https://stackoverflow.com/questions/15338375/android-contentresolver-query-always-returns-same-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!