0

I'm a beginner android programmer and want to implement a list with RecyclerView that can hold any media. If size of image that is loading in my app is large I want to make it smaller. For that I found some code:

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

In first line I'm reading an image in my phone and in second line making it smaller. For writing it I use this part of code:

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

These two processes for large images need about 100 MB memory in ram for about 1-2 seconds. But my problem is here that sometimes after end of these processes memory releases and sometimes not releases. I did this process in many ways but my problem didn't solved. I did it with Handler or Thread. My application has no problem and continue working but I don't like this for my app.

Do you have any idea?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
P7928Z
  • 5
  • 2

1 Answers1

0

You should recycle your Bitmap after every process should be killed.

You can use

bitmap.recycle();

bitmap = null;

  • Thank you very much. It was very useful and good but not complete. I searched about what you said and found these two that were my complete answear: 1- https://stackoverflow.com/questions/3823799/android-how-does-bitmap-recycle-work 2- https://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966 – P7928Z Mar 06 '20 at 08:02