Working with large bitmaps ?

ozgureffe

Member
Licensed User
Longtime User
Hi there
I have a program which uses a scrollable large panel with a large background-image stored in sd card.

It is OK when the program initialize panel for the first time with its background.

But the background image changes with DrawBitmap, programatically and it has to refresh background image after it changes. (Average size of background-image ≈ 200KB )

It works also fine for first 2-3 refreshes but after that it crashes with Out of memory error.

Does device keep first image files even after re-initializing image with new ones? If yes is there any way of helping device to forget first image files to free the memory?

B4X:
Sub RefreshPanel
    paneloFullBgImg.InitializeSample(File.DirRootExternal , "/myapps/temp/bg.png",800%x,50%y)
    panelo.SetBackgroundImage(paneloFullBgImg)
End Sub
 
Last edited:

margret

Well-Known Member
Licensed User
Longtime User
Out of Memory

Hello,

One thing I would try is to only load one graphic to the panel. Then when you want it to change, copy the new image file to the same file name as the graphic which is loaded in the panel. Then call:

Panelo.Invalidate

That should redraw the panel and all the views/controls under the same name and may help with the out of memory issue. I am using something like this in one of the programs I have written and it seems to work even after changing the images more than 100 times. Just a thought.

Margret
 
Upvote 0

ozgureffe

Member
Licensed User
Longtime User
Thank you margret,

Do you mean,
I must use an imageview on my panel instead of using that image file as background-image of panel
and invalidate my imageviewto refresh it?

I think i am missing something in your approach.

Related parts of my code

B4X:
[COLOR=RoyalBlue]
Sub[COLOR=DarkSlateGray] Globals[/COLOR][/COLOR]
    [COLOR=RoyalBlue]Dim [/COLOR]paneloImg [COLOR=RoyalBlue]As[/COLOR] [COLOR=Olive]ImageView[/COLOR]
    [COLOR=RoyalBlue]Dim [/COLOR]panelo [COLOR=RoyalBlue]As[/COLOR] [COLOR=Olive]Panel[/COLOR]
    ...
[COLOR=RoyalBlue]End Sub[/COLOR]
  
[COLOR=RoyalBlue]Sub [COLOR=DarkSlateGray]Activity_Create[/COLOR][/COLOR]
    ...
    paneloImg.Bitmap = [COLOR=RoyalBlue]LoadBitmap[/COLOR]([COLOR=RoyalBlue]File[/COLOR].DirRootExternal , "[COLOR=DarkRed]/myapps/temp/bg.png"[/COLOR])
    ...
[COLOR=RoyalBlue]End Sub[/COLOR]
   
[COLOR=SeaGreen][COLOR=Green]'After changing bg.png manually (same file-name but different image)[/COLOR]
[/COLOR][COLOR=RoyalBlue]Sub [COLOR=DarkSlateGray]refreshPanel[/COLOR][/COLOR]
    paneloImg.Invalidate [COLOR=Green]'panelo.Invalidate[/COLOR]
    [COLOR=RoyalBlue]ToastMessageShow[/COLOR]("[COLOR=DarkRed]Invalidated paneloImg[/COLOR]",[COLOR=RoyalBlue]False[/COLOR])
[COLOR=RoyalBlue]End Sub[/COLOR]
[COLOR=SeaGreen][COLOR=Green]'But nothing changes old bg.png is still on the screen!

[/COLOR][/COLOR]
I put paneloImg inside of panelo both in runtime and with layout designer GUI
but none of them worked.


What do you think about removeView and addView again?
EDIT : Removing and adding view again didnt worked!
 
Last edited:
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Set Image

I was thinking something like this:


B4X:
Sub Globals
    Dim paneloImg As ImageView
    Dim panelo As Panel
    ...
End Sub
  
Sub Activity_Create
    ...
    If FirstTime Then
      SetImage
    End If   
    ...
End Sub
   
'After changing bg.png manually (same file-name but different image)
Sub refreshPanel
    paneloImg.Invalidate 'panelo.Invalidate
    ToastMessageShow("Invalidated paneloImg",False)
End Sub
'But nothing changes old bg.png is still on the screen!

Sub SetImage
   paneloImg.Bitmap = LoadBitmap(File.DirRootExternal , "/myapps/temp/bg.png")
   refreshPanel
End Sub

Sub SelectImage(FileName As String)
   'FileName = "/myapps/temp/newfile.png"
   File.Copy(File.DirRootExternal, FileName, File.DirDefaultExternal, "/myapps/temp/bg.png")
   SetImage
End Sub

Margret
 
Upvote 0

ozgureffe

Member
Licensed User
Longtime User
Thank you Margret,

My problem solved after your help.

Copy file instead of replace , I couldn't get the logic but it is working now without low memory errors.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…