I'm using the following code to delete an image. It works the first time, but when I try to capture an image and delete it
I get a StaleDataException
:
08-07 14:57:24.156: E/AndroidRuntime(789): java.lang.RuntimeException: Unable to
resume activity {com.example.cap_im/com.example.cap_im.MainActivity}:
android.database.StaleDataException: Attempted to access a cursor after it has been closed.
public void deleteImageFromGallery(String captureimageid) {
Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
getContentResolver().delete(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
BaseColumns._ID + "=?", new String[] { captureimageid });
String[] projection = { MediaStore.Images.ImageColumns.SIZE,
MediaStore.Images.ImageColumns.DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATA, BaseColumns._ID, };
Log.i("InfoLog", "on activityresult Uri u " + u.toString());
try {
if (u != null) {
cursor = managedQuery(u, projection, null, null, null);
}
if ((cursor != null) && (cursor.moveToLast())) {
int i = getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
BaseColumns._ID + "=" + cursor.getString(3), null);
Log.v(TAG, "Number of column deleted : " + i);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…