我在Android中收到StrictMode報告的以下違規行爲。StrictMode抱怨InputStream未關閉
4月2日至5日:07:41.190:ERROR/StrictMode(15093):資源在連接的堆棧跟蹤獲取 但從未發佈。有關避免資源泄漏的信息,請參見java.io.Closeable。 4月2日至五日:07:41.190: ERROR/StrictMode(15093):java.lang.Throwable中:顯式的終止 方法 '關閉' 不叫
據惡癖約不正確關閉流。但是,不應該關閉in
關閉底層流?標記錯誤的原因是什麼?
private ArrayList<Uri> loadPath() {
ArrayList<Uri> uris = new ArrayList<Uri>();
if (mFile.exists()) {
ObjectInputStream in = null;
try {
in = new ObjectInputStream(new BufferedInputStream(
new FileInputStream(mFile), STREAM_BUFFER_SIZE));
ArrayList<String> strings = new ArrayList<String>();
strings.addAll((ArrayList<String>) in.readObject());
for (String string : strings) {
uris.add(Uri.parse(string));
}
} catch (Exception e) {
mFile.delete();
} finally {
IOUtils.closeQuietly(in);
}
}
return uris;
}
public static void closeQuietly(InputStream input) {
try {
if (input != null) {
input.close();
}
} catch (IOException ioe) {
// ignore
}
}
不知道StrictMode檢查器有多聰明,但它看起來像被***延遲關閉***弄糊塗了,即使用一個實用程序爲您關閉流。 – Perception 2012-03-01 12:08:51
在我的情況下,即使在'finally'子句中內聯'close()',我也會得到這個錯誤。 – 2012-09-24 13:26:42