【问题标题】:Explicitly closing FileInputStream显式关闭 FileInputStream
【发布时间】:2016-03-21 16:13:58
【问题描述】:

我正在使用 BitmapFactory 从文件中获取位图。 decodeStream 中的第一个参数是一个流。我为它提供了一个新的 FileInputStream。我是否需要明确关闭流?这会导致内存泄漏吗?

Bitmap bm = BitmapFactory.decodeStream(new FileInputStream(x))

也许应该是?:

FileInputStream fis = new FileInputStream(x);
Bitmap bm = BitmapFactory.decodeStream(fis);
fis.close();

我知道位图有泄漏的倾向,这个问题更多是关于 FileInputStream 而不是位图。

【问题讨论】:

    标签: java android fileinputstream


    【解决方案1】:

    是的,您需要关闭流。因为解码函数不知道没有更多的数据要写入,所以它不会为你做。不这样做不太可能导致大的内存泄漏,但如果流被缓冲,可能会导致数据无法刷新到磁盘,并且会导致操作系统资源(至少是文件句柄)未释放。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      • 2010-09-14
      • 2012-12-12
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 2019-03-02
      相关资源
      最近更新 更多