memory problems

Smee

Well-Known Member
Licensed User
Longtime User
I am using the code from Klaus's Scrollview example to show thumbnail images from jpegs in multiple directories

The first time the sub runs it is fine but when i change directories to show new thumbnails i get a crash. Memory error i think.
How can i release all the memory used each time a directory is changed
Thanks


B4X:
Sub LoadImages
   Dim files As List
   Dim imagesFolder As String

   If Bitmaps.IsInitialized=False Then
      Bitmaps.Initialize
      FileNames.Initialize
   Else
      Bitmaps.Clear
      FileNames.Clear
   End If
   
   imagesFolder =RootDirectory & lvSource.GetItem(SourceDirNo)
   files = File.ListFiles(imagesFolder) 'get all files in this folder
   
   For i = 0 To files.Size - 1
      DoEvents 'required for the ProgressDialog animation
      Dim f As String
      f = files.Get(i)
      Log(i)
      FileNames.Add(f)
      Dim b As Bitmap
      b.InitializeSample(imagesFolder, f, 200dip, 200dip) 'load the jpeg file and subsample it if it is too large.
      Bitmaps.Add(b) 'add the bitmap to the bitmaps list.
   Next

   ScrollView1.Panel.Height = 200dip * Bitmaps.Size 'Set the inner panel height according to the number of images.
   For i = 0 To Bitmaps.Size - 1
      Dim iv As ImageView 'create an ImageView for each bitmap
      iv.Initialize("ImageView") 
      Dim bd As BitmapDrawable
      bd.Initialize(Bitmaps.Get(i))
      iv.Background = bd 'set the background of the image view.
      'add the image view to the scroll bar internal panel.
      ScrollView1.Panel.AddView(iv, 5dip, 5dip + i * 200dip, ScrollView1.Width - 10dip, 190dip)
      iv.Tag=i
   Next

End Sub
 

Smee

Well-Known Member
Licensed User
Longtime User
No I did not removew the images. I thought that when the sub run subsequent times then the memory was release. Obviously not.

How could i remove the images?

i tried this

For i=ScrollView1.panel.NumberOfViews To 0 Step -1
ScrollView1.panel.RemoveViewAt(i)
Next

but no good

Thanks for any replies

gOT IT

For i=ScrollView1.panel.NumberOfViews-1 To 0 Step -1
ScrollView1.panel.RemoveViewAt(i)
Next

Thankls Klaus
 
Last edited:
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
It seems i can only load 100 images before i get a crash. Is there a way to overcome this?
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
crashes on the first run if the files are inm excess of 100. I was able to fix it by knocking 50 off each of the 200's

eg b.InitializeSample(imagesFolder, f, 150dip, 150dip)

but it obviously does not really fix it just allows me to load more images. Up to 150
 
Upvote 0

eps

Expert
Licensed User
Longtime User
You need to look at "bumpscrolling"/paging or possibly thumbnails or bitmapsample (apols if you are doing so already)..

By bumpscrolling or paging I mean you only need 3 "pages" worth of images..

1st time.

Load page 1 and page 2.. Displaying page 1.

User scrolls to page 2.. Load Page 3, displaying page 1.

User scrolls back to page 1, unload page 3 and display page 1.

User scrolls to page 2.. Load Page 3, displaying page 1.

User scrolls to page 3.. Load Page 4, unload page 1, displaying page 3.

Or something along those lines, of course a "page" can be whatever you define it as.
 
Upvote 0
Top