B4A Library BETA: VBBitmap (extended safer LoadBitmapSample)

Releasing VBBitmap 1.3 beta

  • SmartBitmap
    Permissions:
    • android.permission.WRITE_EXTERNAL_STORAGE
    Properties:
    • GlobalAllocSize As Long
      returns getGlobalAllocSize;
    • GlobalFreedSize As Long
      returns getGlobalFreedSize;
    Methods:
    • FreeMem As Double
      returns free memory available from heap
      double memoryAvailable = Runtime.getRuntime().maxMemory() - (heapSize - heapRemaining) - nativeUsage;
    • FreeMem2 As Long
      returns freeMemory
      long freeMemory = (Runtime.getRuntime().maxMemory()) - (Debug.getNativeHeapAllocatedSize());
    • Garbage
      Runs the Garbage Collector to free memory
      Make sure your code is good first you are not initializing bitmaps aggressively
      No amount of System.gc() calls will help a memory leak
      You should split up the work and cache your data to file(s) and remove objects accordingly.
      If you really need more memory you should run your java app with larger heap memory set
    • HowBig (Directory As String, Filename As String) As String
      HowBig(String Directory, String Filename)
      returns height/width as one combined string variable
    • MaxMem As Double
      returns MaxMemory
      Runtime.getRuntime().maxMemory()
    • heapRemaining As Double
      returns heapRemaining
      Runtime.getRuntime().freeMemory();
    • heapSize As Double
      returns totalMemory
      Runtime.getRuntime().totalMemory();
    • loadbitmap (Directory As String, Filename As String, reqWidth As Integer, reqHeight As Integer) As Bitmap
      returns a simple bitmap
      dont use file.dirassets ever, copy image over to another place from project
      example: Activity.loadbitmap(mm.GetSafeBitmap(File.DirInternal,"/moon.jpg",Activity.height,Activity.width))
      If this runs into memory problems, it's going to load a really low res image which will look bad
      but it might not crash the app
    • loadbitmap2 (Directory As String, Filename As String, SampleSize As Integer, RGB565 As Boolean) As Bitmap
      loadbitmap2(String Directory, String Filename, Integer SampleSize, Boolean RGB565)
      returns a safe sized bitmap
      dont use file.dirassets ever, copy image over to another place from project
      example: Activity.SetBackgroundImage(mm.loadbitmap2(File.DirInternal,"/moon.jpg",8))
      SampleSize, can be 2, 4, ,6, 8,....16, 32...etc
      RGB565=True will set options.inPreferredConfig = Config.RGB_565;
    • nativeUsage As Double
      returns free memory available from heap
      Debug.getNativeHeapAllocatedSize();

BitmapLib.HowBig - returns the height/width of an image
Msgbox(BitmapLib.HowBig(File.DirInternal,"/moon3.jpg"),"SIZE")



BitmapLib.loadbitmap2 This allows you to pass a SampleSize
Also lets you choose loading the bitmap as 16bit RGB565 which also could save some memory

you can do the math yourself to find the best SampleSize and read up here: Loading Large Bitmaps Efficiently | Android Developers

loadbitmap2(String Directory, String Filename, Integer SampleSize, Boolean RGB565)

*Setting this Boolean RGB565 flag to true, acts like a converter to convert the 24bit image file into a 16bit


BitmapLib.loadbitmap Will act like LoadBitmapSample, but if it runs into an out of memory it will automatically load a really low res image so your app won't crash and you get the bad app review on Google Play Market from one of the 1500+ devices out there. ( You never know what a device has running and how much memory is available when your app is loaded. )

loadbitmap(String Directory, String Filename, Integer Width, Integer Height)


BitmapLib.Garbage - Runs the Garbage Collection



Information about Memory


BitmapLib.FreeMem - returns free memory available from heap double memoryAvailable = Runtime.getRuntime().maxMemory() - (heapSize - heapRemaining) - nativeUsage

BitmapLib.FreeMem2 - returns freeMemory long freeMemory = (Runtime.getRuntime().maxMemory()) - (Debug.getNativeHeapAllocatedSize());

BitmapLib.heapRemaining - returns heapRemaining Runtime.getRuntime().freeMemory();

BitmapLib.heapSize - returns totalMemory Runtime.getRuntime().totalMemory();

BitmapLib.MaxMem - returns MaxMemory Runtime.getRuntime().maxMemory()

BitmapLib.nativeUsage - returns free memory available from heap Debug.getNativeHeapAllocatedSize();


BitmapLib.GlobalAllocSize
BitmapLib.GlobalFreedSize







Example usage:
B4X:
Dim BitmapLib As SmartBitmap
File.Copy(File.DirAssets,"moon3.jpg",File.DirInternal,"moon3.jpg")
        Activity.SetBackgroundImage(BitmapLib.loadbitmap2(File.DirInternal,"/moon3.jpg",1,True))
      'Activity.SetBackgroundImage(BitmapLib.loadbitmap(File.DirInternal,"/moon3.jpg",Activity.Width,Activity.Height))
Msgbox(BitmapLib.HowBig(File.DirInternal,"/moon3.jpg"),"SIZE")
Msgbox(BitmapLib.heapRemaining,"HeapRemaining")

Please test with some big images/bitmaps.


Thanks in Advance

Vb1992

Library Revision History:

Old...... Library is version 1.0 - June 29th, 2012
Old...... Library is version 1.1 - June 29th, 2012
Old...... Library is version 1.2 - July 2nd, 2012
New...... Library is version 1.3 - July 2nd, 2012
improved algorithmic values for loadbitmap
added Log( GlobalAllocSize)
added Log( GlobalFreedSize)
 

Attachments

  • VBBitmap1.3.zip
    5.4 KB · Views: 988
Last edited:

westingenieria

Active Member
Licensed User
Longtime User
sample project: "File has expired and does not exist anymore on this server"... i need this.. thanks
 
Top