我有一個遊標,我在下面的代碼中使用。但是我想在光標被使用並且不再需要之後關閉光標。問題是遊標被用在return語句中,但是在返回語句後我無法關閉它,因爲這是無法訪問的代碼。它在return語句中使用,所以我無法在該行之上關閉它。我如何關閉光標?這不像舊的managedQuery,我認爲你必須關閉它。在哪裏關閉遊標?
public String getPath(Uri uri) {
String[] projection = { MediaStore.Audio.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
// cursor.close() <--- not possible because it is unreachable code after return
}
我忘了在我原來的帖子中添加。這是在一個服務,而不是一個活動,所以我想我不能在一個服務中使用startManagingCursor()。 – Kevik