2013-01-15 26 views
1

我每次完成一個包含有ArrayAdapter一個GridView中,我從getView(返回ImageView的應用程序),我得到一個錯誤,指出:ImageView的setImageURI導致strictmode錯誤的onPause

01-15 17 :28:55.715:E/StrictMode(6480):資源是在附加的堆棧跟蹤中獲取的,但從未釋放。有關避免資源泄漏的信息,請參閱java.io.Closeable。 E/StrictMode(6480): java.lang.Throwable中:顯式的終止方法 '關閉' 不叫

這個問題似乎指向幾行:

imageView.setImageURI(uri)

任何想法?

回答

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); 
} 
+0

謝謝richardleggett - 完美的工作(在發生問題幾周後,無法找到原因!) –