如果没有,如何在使用后释放大内存用于处理位图?

How to release big memory for working with bitmaps after use if it didn't?

我是初学者 android 程序员,想用 RecyclerView 实现一个可以容纳任何媒体的列表。 如果在我的应用程序中加载的图像尺寸很大,我想将其缩小。为此,我找到了一些代码:

Bitmap bitmap = BitmapFactory.decodeFile(path);
Bitmap smallerBitmap = ThumbnailUtils.extractThumbnail(bitmap, bitmap.getWidth() / 8, bitmap.getHeight() / 8));

在第一行中,我正在阅读 phone 中的图像,在第二行中,我将其缩小。为了编写它,我使用了这部分代码:

try (FileOutputStream out = new FileOutputStream(finalMyFile)) {
     smallerBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
} catch (IOException e) {
     e.printStackTrace();
}

这两个处理大图像的过程需要大约 100 MB 内存在 ram 中大约 1-2 秒。但我的问题在于,有时在这些进程结束后内存会释放,有时不会释放。 我以多种方式完成了这个过程,但我的问题没有解决。我是用 HandlerThread 做的。 我的应用程序没有问题并继续工作,但我不喜欢我的应用程序。

你有什么想法吗?

您可以使用此 https://square.github.io/leakcanary/ 找到答案

您应该在终止每个进程后回收您的位图。

您可以使用

bitmap.recycle();

bitmap = null;