关于android:将Bitmap解码为ImageView而不缓存,防止内存泄漏 | 珊瑚贝

Decoding Bitmap to ImageView without caching, preventing memory leaks


所以我在 http://developer.android.com/training/displaying-bitmaps/index.html 上阅读了所有关于 Bitmap 的教程,似乎他们在一个非常特定的级别使用类来管理它们的缓存。我也知道有像 Universal Image Loader 这样的库可以为您解决这个问题。

但是让我们变得简单,如果我只想从文件中解码单个 Bitmap 并将其放在 ImageView 上怎么办。据我所知,为了防止内存泄漏,您不应该保留对 Bitmap 的引用,那么如何实现这一点。假设位图的文件路径存储在以下字符串中:imagePath.


只要在不再需要 Bitmap 时(例如,当 Activity 被销毁时)进行清理,就可以保留对 Bitmap 对象的引用。

为了确保您没有任何与 Bitmap 相关的泄漏泄漏:

1
2
3
imageView.setImageBitmap(null);
bitmap.recycle();    // frees the Bitmap instance
bitmap = null;

1
2
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); //File file
iv.setImageBitmap(bitmap); // ImageView iv


来源:https://www.codenong.com/23913032/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9279.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?