Hello,
I'm putting together a simple flashcard app where I'm loading multiple bitmaps and images and receiving the lack of memory error as the app crashes. From what I can gather from the forums there seems to be a consensus that using multiple bitmaps and images often yields this result. In the app I have a different activity for each topic. As I move through the activities I think loaded bitmaps from previous activities remain stored in the memory, so it doesn't take long for the app to crash. I'm not sure how to solve this problem or if that is even the issue.
I've posted the code from one the activities to illustrate how I've set up my code. I'm sure there is a more eloquent and efficient way to write the below code that will solve the lack of memory problem, so I welcome any tips and suggestions.
I'm putting together a simple flashcard app where I'm loading multiple bitmaps and images and receiving the lack of memory error as the app crashes. From what I can gather from the forums there seems to be a consensus that using multiple bitmaps and images often yields this result. In the app I have a different activity for each topic. As I move through the activities I think loaded bitmaps from previous activities remain stored in the memory, so it doesn't take long for the app to crash. I'm not sure how to solve this problem or if that is even the issue.
I've posted the code from one the activities to illustrate how I've set up my code. I'm sure there is a more eloquent and efficient way to write the below code that will solve the lack of memory problem, so I welcome any tips and suggestions.
B4X:
Sub Globals
Private svp As StdViewPager
Private BearImage As ImageView
Private BirdImage As ImageView
Private CatImage As ImageView
Private ChickenImage As ImageView
Private CowImage As ImageView
Private DogImage As ImageView
Private ElephantImage As ImageView
Private FishImage As ImageView
Private HorseImage As ImageView
Private LionImage As ImageView
Private MonkeyImage As ImageView
Private SheepImage As ImageView
Private PigImage As ImageView
End Sub
Sub Activity_Create(FirstTime As Boolean)
svp.Initialize("svp",13,100%x,100%y)
Activity.AddView(svp.AsView,0,0,100%x,100%y)
svp.Panels(0).LoadLayout("Bear.bal")
svp.Panels(1).LoadLayout("Bird.bal")
svp.Panels(2).LoadLayout("Cat.bal")
svp.Panels(3).LoadLayout("Chicken.bal")
svp.Panels(4).LoadLayout("Cow.bal")
svp.Panels(5).LoadLayout("Dog.bal")
svp.Panels(6).LoadLayout("Elephant.bal")
svp.Panels(7).LoadLayout("Fish.bal")
svp.Panels(8).LoadLayout("Horse.bal")
svp.Panels(9).LoadLayout("Lion.bal")
svp.Panels(10).LoadLayout("Monkey.bal")
svp.Panels(11).LoadLayout("Sheep.bal")
svp.Panels(12).LoadLayout("Pig.bal")
Dim bmp1 As Bitmap = ResizeImage(LoadBitmapSample(File.DirAssets, "Bear.jpg",Activity.Width,Activity.Height), 100%x, 100%y)
Dim bmp2 As Bitmap = ResizeImage(LoadBitmapSample(File.DirAssets, "Bird.jpg",Activity.Width,activity.Height), 100%x, 100%y)
Dim bmp3 As Bitmap = ResizeImage(LoadBitmapSample(File.DirAssets, "Cat.jpg",Activity.Width,activity.Height), 100%x, 100%y)
Dim bmp4 As Bitmap = ResizeImage(LoadBitmapSample(File.DirAssets, "Chicken.jpg",Activity.Width,activity.Height), 100%x, 100%y)
Dim bmp5 As Bitmap = ResizeImage(LoadBitmapSample(File.DirAssets, "Cow.jpg",Activity.Width,activity.Height), 100%x, 100%y)
Dim bmp6 As Bitmap = ResizeImage(LoadBitmapSample(File.DirAssets, "Dog.jpg",Activity.Width,activity.Height), 100%x, 100%y)
Dim bmp7 As Bitmap = ResizeImage(LoadBitmapSample(File.DirAssets, "Elephant.jpg",Activity.Width,activity.Height), 100%x, 100%y)
Dim bmp8 As Bitmap = ResizeImage(LoadBitmapSample(File.DirAssets, "Fish.jpg",Activity.Width,activity.Height), 100%x, 100%y)
Dim bmp9 As Bitmap = ResizeImage(LoadBitmapSample(File.DirAssets, "Horse.jpg",Activity.Width,activity.Height), 100%x, 100%y)
Dim bmp10 As Bitmap = ResizeImage(LoadBitmapSample(File.DirAssets, "Lion.jpg",Activity.Width,activity.Height), 100%x, 100%y)
Dim bmp11 As Bitmap = ResizeImage(LoadBitmapSample(File.DirAssets, "Monkey.jpg",Activity.Width,activity.Height), 100%x, 100%y)
Dim bmp12 As Bitmap = ResizeImage(LoadBitmapSample(File.DirAssets, "Sheep.jpg",Activity.Width,activity.Height), 100%x, 100%y)
Dim bmp13 As Bitmap = ResizeImage(LoadBitmapSample(File.DirAssets, "Pig.jpg",Activity.Width,activity.Height), 100%x, 100%y)
BearImage.Bitmap = bmp1
BirdImage.Bitmap = bmp2
CatImage.Bitmap = bmp3
ChickenImage.Bitmap = bmp4
CowImage.Bitmap = bmp5
DogImage.Bitmap = bmp6
ElephantImage.Bitmap = bmp7
FishImage.Bitmap = bmp8
HorseImage.Bitmap = bmp9
LionImage.Bitmap = bmp10
MonkeyImage.Bitmap = bmp11
SheepImage.Bitmap = bmp12
PigImage.Bitmap = bmp13
End Sub
Sub ResizeImage(original As Bitmap, TargetX As Int, TargetY As Int) As Bitmap
Dim origRatio As Float = original.Width / original.Height
Dim targetRatio As Float = TargetX / TargetY
Dim scale As Float
If targetRatio > origRatio Then
scale = TargetY / original.Height
Else
scale = TargetX / original.Width
End If
Dim c As Canvas
Dim b As Bitmap
b.InitializeMutable(TargetX, TargetY)
c.Initialize2(b)
'set the background
c.DrawColor(Colors.LightGray)
Dim r As Rect
Dim w = original.Width * scale, h = original.Height * scale As Int
r.Initialize(TargetX / 2 - w / 2, TargetY / 2 - h / 2, TargetX / 2 + w / 2, TargetY / 2+ h / 2)
c.DrawBitmap(original, Null, r)
Return b
End Sub