My Activity contains this code to get all images on the SD card:
String[] projection = {MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA,
MediaStore.Images.ImageColumns.DATA};
Cursor cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, null, null,
MediaStore.Images.Media._ID);
int count = cursor.getCount();
int image_path_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
int i;
for(i = 0; i < count; i++) {
cursor.moveToPosition(i);
String p = cursor.getString(image_path_index);
fd.addToPhonePhoto(p);
}
cursor.close();
The occurred while the Activity was resuming:
03-14 14:06:48.380: E/AndroidRuntime(20793): java.lang.RuntimeException: Unable to resume activity {}: java.lang.RuntimeException: Unable to resume activity {}: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
It only occurs on Android 4.0. If on Android 2.x or 3.x, it works normally. But if I change the system setting which selects the "don't keep Activities" option in "Developer options". The error does not show.
I want to modify my code to avoid this error without changing the system setting. How should I do it?