Completely Stumped

sterlingy

Active Member
Licensed User
Longtime User
Folks,

I've studied this code for a while, and I can't seem to understand why I keep getting a Null Point Error when I try to add an ImageView to the ScrollView.

In debug mode, it appears that all objects are properly assigned.

Anyone have a clue?

B4X:
Sub LoadImages
   
   Bitmaps.Initialize
   Dim files As List
   Dim imagesFolder As String
   imagesFolder = File.DirRootExternal & "/MySO/photos"
   If File.Exists(imagesFolder, "") = False Then
      ToastMessageShow("Images folder not found: " & CRLF & imagesFolder, True)
      Return
   End If

   files = File.ListFiles(imagesFolder) 'get all files in this folder
   ScrollView1.Initialize(200dip*files.size)
   ScrollView1.Panel.Height = 200dip * files.Size 'Set the inner panel height according to the number of images.
Log("# files:" & files.Size)   
   For i = 0 To files.Size - 1
      DoEvents 'required for the ProgressDialog animation
      Dim f As String
      Dim newView As ImageView
      f = files.Get(i)
      If f.ToLowerCase.EndsWith(".jpg") Then
         Dim b As Bitmap
         b.InitializeSample(imagesFolder, f, 200dip, 200dip) 'load the jpeg file and subsample it if it is too large.
         newView.Initialize("newView")
         newView.Bitmap = b
         newView.Gravity = Gravity.FILL
         newView.Tag = f
Log("Bitmap:"& newView.Bitmap)
         ScrollView1.Panel.AddView(newView, 5dip, 5dip + i * 200dip, ScrollView1.Width - 10dip, 190dip)
         ScrollView1.Height = 100%y - 10dip
      End If
   Next
   ToastMessageShow("Found " & Bitmaps.Size & " images", True)
End Sub
 

sterlingy

Active Member
Licensed User
Longtime User
Erel,

See attached. Once I get it working, I will break up the code to make it more modular, but I have to solve this problem first.

Thanks for your help.

-Sterling
 

Attachments

  • MySOG1.zip
    7.5 KB · Views: 266
Upvote 0

klaus

Expert
Licensed User
Longtime User
Attached you find a modified version of your program.
As ScrollView1 is defined in the Designer in the layout file you must NOT initialize in the code.
I removed the filling of the ScrollView from the LoadImages routine and defined a specific FillScrollView routine in Activity_Resume.

Best regards.
 

Attachments

  • MySOG2.zip
    7.5 KB · Views: 283
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
Klaus,

Thanks for not only solving the problem, but cleaning up my code. One thing however, I get an error after 31 images are loaded:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

Is there a way to increase the memory allocation?


-Sterling
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
I'm afraid not.
Try to load with LoadBitmapSample instead of LoadBitmap.

Klaus,

I'm trying to load as many images as I have in the folder, which could become hundreds. Essentially, I want to create a gallery of images, which I can click on to show fullscreen.

Forgive my ignorance, but LoadBitmap isn't employed in the current code, so I'm not sure where LoadBitmapSample would go.

Currently, I am using initializeSample. Maybe this just scales the image, without reducing the memory overhead.

-Sterling
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Sorry, I answered without looking at the code.
Using InitializeSample is the same as LoadBitmapSample, so you are already loading image samples and not the full images.

So to get more images on the screen you must reduce the size of the images in the ScrollView.

Best regards.
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
Klaus,

How does my phone's standard gallery support over 500 photos?

-Sterling
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
I suppose that they don't load all images at the same time.

Klaus,

You could be right, however, I think the phone does load thumbnails of all photos. I have 994 images on my phone. At any given moment, 28 thumbnails are visible and scrolling through the entire collection is incredibly smooth and very fast, which leads me to believe that the gallery app isn't loading and unloading thumbnails on the fly.

-Sterling
 
Last edited:
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Klaus,

You could be right, however, I think the phone does load thumbnails of all photos. I have 994 images on my phone. At any given moment, 28 thumbnails are visible and scrolling through the entire collection is incredibly smooth and very fast, which leads me to believe that the gallery app isn't loading and unloading thumbnails on the fly.

-Sterling
I am fairly sure it is loading on the fly.
Firstly they are load/unloading thumbnails which consume less memory and is quicker.
Secondly, no device has an infinite amount of memory.
On my device, if I flick right/left really hard I do see the thumbnails load after a second. It usually caches the next few pages, so you have to scroll really fast to see the effects.
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
Can you tell me more about load/unload thumbnails? I've never heard of this

-Sterling
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Can you tell me more about load/unload thumbnails? I've never heard of this

-Sterling
I am not sure what you mean. You havent heard of loading/unloading thumbnails?
So when the gallery loads up, it starts to load the thumbnails of the images on the screen, and possibly the images on the next few screens to make scrolling smooth. However, when you scroll past a given threshold, it starts to unload the thumbnails from behind (it just frees up the memory) so that it can load more thumbnails in the forward direction. This is necessary since there is an upper memory limit.
Assuming a 128x128 thumbnail bitmap (64KB), you can load upto 256 thumbnail bitmaps in 16MB of RAM (which is usually the process memory limit).
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
I am not sure what you mean. You havent heard of loading/unloading thumbnails?

I understand the concept, but I thought you meant there is a Method in B4A that does this. Something like GalleryView.UnloadThumbnails(1,50)

-Sterling
 
Upvote 0
Top