Android的ContentResolver.query总是返回相同的数据 [英] Android ContentResolver.query always returns same data

查看:478
本文介绍了Android的ContentResolver.query总是返回相同的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有filebrowser上市SD卡上的所有视频录像机的应用程序

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

code。通过我希望得到SD音频文件卡

使用ContentResolver的,按预期方式工作,但它不如果文件上卡变化更新。我并不自动意味着,但查看/应用程序重新启动后。甚至没有帮助重新安装应用程序,仍显示相同的文件。被删除的视频文件通过PC是不可见的,也有可能发挥它(该视频无法播放(翻译))。

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

编辑1 [14-03-2013]:

Edit 1 [14-03-2013]:

我尝试添加号+=+号订购或WHERE子句作为一个潜在的查询缓存克星,但它没有任何效果(尽管它可能是被一个优化作为一个无用的条款删除)。这一次,我一直在使用不同的证书重​​新安装从不同的计算机应用程序,但查询结果保持不变,目前上市不存在的文件。

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.

推荐答案

您应该首先调用 cursor.moveToFirst()

所以,你的光标迭代循环应该像

So, your cursor iteration loop should look like

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

这篇关于Android的ContentResolver.query总是返回相同的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆