1

Every time I finish an application that contains a GridView with an ArrayAdapter in which I return an ImageView from getView() I get an error that says:

01-15 17:28:55.715: E/StrictMode(6480): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. E/StrictMode(6480): java.lang.Throwable: Explicit termination method 'close' not called

The problem appears to point to several lines with:

imageView.setImageURI(uri)

Any ideas?

4

1 回答 1

4

在某些情况下 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);
}
于 2013-01-15T17:40:15.413 回答