Bitmapsample vs. Bitmap?

Bill Norris

Active Member
Licensed User
Longtime User
Have read about using bitmapsample vs. bitmap for larger images. My question: if the bitmap is already sized to the exact dimensions of the imageview, is there any benefit (i.e, less memory) to using bitmapsample?
 

margret

Well-Known Member
Licensed User
Longtime User
If all of your images are of the correct size and the user is not allowed to add images that may be larger, you could just use Bitmap. If they can add images that will be displayed, I would use the BitmapSample in case their image is larger. When testing, I have not found any difference between the samples it scales and the ones I reduce myself.
 
Last edited:
Upvote 0

Bill Norris

Active Member
Licensed User
Longtime User
RE:

Thanks for that. I am continuing to battle with outofmemory errors. I have one activity with scrolling images that appears to be the culprit. There are 12 images, 600x700, that load with the activity starts. I think I have exhausted all the remedy attempts, including recycle bitmap , which I don't really understand what it is but I implemented it anyway. Have also tried setting bitmap=null for the 12 imageviews when leaving the activity -- it resolves the problem but causes approx 1 second delay before the next activity displays. Not a big deal, but might be annoying to end user. Think I'm going to have to go back to loading one image at a time and unloading as needed.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
The size in memory will be 600x700x4=1.6MB
12 images is close to 19.2 MB
Depending on what device you are running on, you are quite close to the VM Budget.
LoadBitmapSample won't help unless you actually make the image smaller with it because it is the final size in memory that matters.
Given the size of the images, do you need them all loaded at the same time?

Sent from my GT-I9000 using Tapatalk 2
 
Upvote 0

vb1992

Well-Known Member
Licensed User
Longtime User
I am not sure if the OS library here gives this information
but this might help you during your testing to see memory

B4X:
Dim obj1 as reflector 
   
   Dim ah, fh, gnhs As Long
Dim args(0) As Object
Dim types(0) As String

'  Debug.getNativeHeapAllocatedSize

ah = obj1.RunStaticMethod("android.os.Debug", "getNativeHeapAllocatedSize", args, types)
fh = obj1.RunStaticMethod("android.os.Debug","getNativeHeapFreeSize", args, types)
gnhs = obj1.RunStaticMethod("android.os.Debug","getNativeHeapSize", args, types)
Msgbox(ah, "ALLOCATED HEAP")
Msgbox(fh, "FREE HEAP")
Msgbox(gnhs, "getNativeHeapSize")
 
Upvote 0

Bill Norris

Active Member
Licensed User
Longtime User
RE:

The KB for the files adds up to 3.54 MB. The idea of having them all loaded at once is to allow the user to scroll through them. I initially had them load one at the time, based on users selection from a menu list adjacent. When I had a few sample users go through the app, when they got to that screen, their first interaction was a swipe on the picture -- because, as they said, "that's what people naturally do when they see a picture". So, I reworked the activity and added the scrollview for the pictures. Well, then came the memory errors, so now I am back to a version of original, with one image at a time.
 
Upvote 0

vb1992

Well-Known Member
Licensed User
Longtime User
Why don't you use a scroll view with 5 images
and the last image looks fancy with a "Load More..."

if they click on that, then you load up 4 more new
images and release the old 4 images with recycle.....

I think the user experience would be acceptable
 
Upvote 0

Bill Norris

Active Member
Licensed User
Longtime User
RE:

Along the lines of your previous post, I am wondering about loading three, then, using scrollposition value, unload and load images accordingly so that the image being viewed, plus the one above and below it, are the only ones loaded at any given time. My concern is that it may create some sluggishness or "choppiness" to the scroll as the load/unload code executes. I don't think the load/unload code will be an issue, but having a bunch if/then statements in the scrollposition.changed sub will definitely gum up the works, because it is continually executing while scrolling. I wish there were something like a scroll.stopped event that would only fire when the scroll ended. I think I'll put that in the B4A wishlist.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
From another thread on here, this link was suggested, Loading Large Bitmaps Efficiently | Android Developers

The main issue is having to handle different device resolutions. It may work well on one device but then not on another, as the OS expands the image fully and then attempts to resize. BitmapSample (as far as my simple understanding of the issue goes) reduces this memory usage and therefore means that you are less likely to run out of memory.

From the link above :


Now that the image dimensions are known, they can be used to decide if the full image should be loaded into memory or if a subsampled version should be loaded instead. Here are some factors to consider:

Estimated memory usage of loading the full image in memory.
Amount of memory you are willing to commit to loading this image given any other memory requirements of your application.
Dimensions of the target ImageView or UI component that the image is to be loaded into.
Screen size and density of the current device.

As most devices are so small anyway, there isn't too much of an issue using BitmapSample as not too much detail is lost anyway.
 
Upvote 0
Top