小编Roo*_*had的帖子

StrictMode抱怨InputStream未被关闭

我在Android中遇到StrictMode报告的以下违规行为.

02-05 04:07:41.190:ERROR/StrictMode(15093):在附加堆栈跟踪处获取资源但从未释放.有关避免资源泄漏的信息,请参阅java.io.Closeable.02-05 04:07:41.190:ERROR/StrictMode(15093):java.lang.Throwable:未调用显式终止方法'close'

关于不正确关闭流是很困难的.但是,不应该关闭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();
            } …
Run Code Online (Sandbox Code Playgroud)

java android objectinputstream android-strictmode

8
推荐指数
2
解决办法
7341
查看次数