【发布时间】:2012-03-19 21:41:19
【问题描述】:
我收到了StrictMode 在 Android 中报告的以下违规行为。
02-05 04:07:41.190: ERROR/StrictMode(15093): 获取资源 在附加的堆栈跟踪中,但从未释放。请参阅 java.io.Closeable 了解 有关避免资源泄漏的信息。 02-05 04:07:41.190: 错误/严格模式(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 检查器有多聪明,但它看起来被您的延迟关闭弄糊涂了,即使用实用程序关闭您的为您直播。
-
在我的情况下,即使
close()在finally子句中内联,我也会收到此错误。
标签: java android objectinputstream android-strictmode