【问题标题】:ImageView setImageURI is causing strictmode error onPauseImageView setImageURI 导致严格模式错误 onPause
【发布时间】:2012-12-29 21:12:54
【问题描述】:

每次我完成一个包含带有 ArrayAdapter 的 GridView 的应用程序时,我从 getView() 返回一个 ImageView,我收到一条错误消息:

01-15 17:28:55.715:E/StrictMode(6480):在附加的堆栈跟踪中获取了资源,但从未释放。有关避免资源泄漏的信息,请参阅 java.io.Closeable。 E/严格模式(6480): java.lang.Throwable:未调用显式终止方法“关闭”

问题似乎指向几行:

imageView.setImageURI(uri)

有什么想法吗?

【问题讨论】:

    标签: android imageview android-strictmode


    【解决方案1】:

    在某些情况下 setImageURI() 打开一个 InputStream 但不会关闭它。这是一个解决方法:

    InputStream is = null;
    try { 
      if(uri != null) {
        is = getContentResolver().openInputStream(uri);
        Drawable d = Drawable.createFromStream(is, null);
        imageView.setImageDrawable(d);
      }
    } catch(Exception e) {
      Log.e(TAG, "Unable to set ImageView from URI: " + e.toString());
    } finally {
      org.apache.commons.io.IOUtils.closeQuietly(is);
    }
    

    【讨论】:

    • 感谢richardleggett - 工作得很好(经过数周的问题并且无法找到原因!)
    猜你喜欢
    • 2012-10-27
    • 2022-06-12
    • 2015-04-29
    • 2015-12-16
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    相关资源
    最近更新 更多