[英]Android ContentResolver.query always returns same data
我有帶文件瀏覽器的視頻播放器應用,其中列出了SD卡上的所有視頻
受我啟發的代碼希望在SD卡中獲取音頻文件
使用ContentResolver可以按預期工作,但是如果卡上的文件發生更改,則不會更新。 我並不是說自動,而是在視圖/應用程序重啟后。 甚至沒有重新安裝應用程序的幫助,仍然顯示相同的文件。 已刪除的視頻文件在PC上不可見,也無法播放(該視頻無法播放(翻譯))。
我轉儲了數據,問題不在視圖緩存或其他地方。 我沒有實現自己的任何緩存,也沒有找到任何有關此問題的信息。 謝謝
碼:
// 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]:
我嘗試在ORDER或WHERE子句中添加number + " = " + number
來充當潛在的查詢緩存破壞者,但是它沒有任何效果(盡管有可能被優化器作為無用的子句刪除了)。 這次我使用不同的證書從另一台計算機上重新安裝了該應用程序,但是查詢結果保持不變,列出了當前不存在的文件。
您應該首先調用cursor.moveToFirst()
。
因此,您的游標迭代循環應如下所示
if (cursor.moveToFirst()) {
do {
// cursorIndex = cursor.getColumnIndexOrThrow, etc...
} while (cursor.moveToNext());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.