-1

I have a RecyclerView in an android app. I am doing some kind of file explorer.

When I plug my own device on the computer, it runs out of memory when I reach the Camera folder in DCIM folder.

This is because there are a lot of pictures in there and I am trying to show them as the ImageView instead of the usual file icon drawable.

When the images are loaded the app runs out of space so it crashes

I implemented the app using a RecyclerView thinking it is going to recycle the views and not run out of memory or lag. But it doesn't seem to work.

I tried doing some kind of workaround by loading images only for the visible portion of the RecyclerView but it requires a lot of twists and it didn't work in the end.

Is there a way to load lot of images and make sure they don't take too much space in RAM. I'm using an old phone and I'd like my app to work on it. It is a Galaxy Ace II.

user123
  • 2,510
  • 2
  • 6
  • 20
  • Are you using any library to display images or handling by yourself? – Attiq ur Rehman Jan 16 '20 at 04:30
  • I'm simply using the line: Drawable drawable = Drawable.createFromPath(pathToImage) – user123 Jan 16 '20 at 04:31
  • 3
    I guess the images are quite big, so you should shrink them before loading them into the ImageView. You can use libraries like Glide, Picasso or UniversalImageLoader to shrink the images for you. – StefanTo Jan 16 '20 at 04:31
  • Is there any native methods to shrink images? – user123 Jan 16 '20 at 04:34
  • 1
    See here: https://developer.android.com/training/displaying-bitmaps/load-bitmap.html – StefanTo Jan 16 '20 at 04:44
  • use glide or picasso for displaying images... – Shivam Oberoi Jan 16 '20 at 04:48
  • Please google first. This problem is as old as the platform – Chisko Jan 16 '20 at 05:05
  • 2
    Does this answer your question? [Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM](https://stackoverflow.com/questions/32244851/androidjava-lang-outofmemoryerror-failed-to-allocate-a-23970828-byte-allocatio) – Chisko Jan 16 '20 at 05:06
  • You dont need to reduce loading for visible part of recyclerview. That's exactly what the RecyclerView does for you. – Ridcully Jan 16 '20 at 05:22

3 Answers3

1

use glide add com.github.bumptech.glide:glide:4.10.0 in your dependencies

Ravinder Kumar
  • 7,407
  • 3
  • 28
  • 54
1

Why handling images in the app, using Glide or Picasso is stronly recommended.

1- To implement Glide https://github.com/bumptech/glide (that I prefer)

2- To implement Picasso https://square.github.io/picasso/#download (also a strong library)

They handle images professionally. They have tools to avoid OOM caused by images. you should follow tutorials to implement and use them.

But OOM is not only because of the images. Whole application must be observed.

https://stackoverflow.com/a/58358334/11982611 this post can help for this.

After implmenting, To make resizing with Glide

Glide.with(mCtxt).load( image uri or url or drawable )
                     .error( image when error occurs )
                .override(320,180) // overrided width and height
                .centerCrop()
                .into(  imageview );
  • Whatever you are using, you should adjust your images according to mobile device
Eren Tüfekçi
  • 2,463
  • 3
  • 16
  • 35
0

Thank you for your answers. I ended up doing the following:

Bitmap bitmap = BitmapFactory.decodeFile(filePath);
bitmap = Bitmap.createScaledBitmap(bitmap, 50, 50, false);
imageView.setImageBitmap(bitmap);

It is still very laggy even at 50x50 pixels. I may try GLIDE library.

user123
  • 2,510
  • 2
  • 6
  • 20
  • You could put the creating of the scaled bitmap in a background thread. Or, as others suggested, use a library like glide or picasso. – Ridcully Jan 16 '20 at 05:24