Android Question Activity.SetBackgroundImage sometimes error OutOfMemory

scsjc

Well-Known Member
Licensed User
I set a background activity with:

B4X:
Activity.SetBackgroundImage(LoadBitmapResize(File.DirAssets,"background.jpg",Activity.width,Activity.height,True))
the image (534px * 949px) is compress on jpg 140kb, real size = 1,45M
sometimes have this error:

B4X:
java.lang.OutOfMemoryError: Failed to allocate a 6788208 byte allocation with 4043752 free bytes and 3MB until OOM
    at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
    at android.graphics.Bitmap.nativeCreate(Native Method)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:879)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:856)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:787)
    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:663)
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Resize(CanvasWrapper.java:568)
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.InitializeResize(CanvasWrapper.java:549)
    at anywheresoftware.b4a.keywords.Common.LoadBitmapResize(Common.java:1371)
    at myworldapp.people.com.privado._activity_create(privado.java:463)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at myworldapp.people.com.privado.afterFirstLayout(privado.java:106)
    at myworldapp.people.com.privado.access$000(privado.java:19)
    at myworldapp.people.com.privado$WaitForLayout.run(privado.java:84)
    at android.os.Handler.handleCallback(Handler.java:836)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:203)
    at android.app.ActivityThread.main(ActivityThread.java:6247)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
any solution?
 

ronell

Well-Known Member
Licensed User
add this to your manifest
B4X:
SetApplicationAttribute(android:largeHeap, "true")
this is the easiest workaround for the issue but not optimal
 

scsjc

Well-Known Member
Licensed User
You are creating a very large image. The memory required is roughly activity width * activity height * 4.

Start with eliminating the scale:
B4X:
Activity.SetBackgroundImage(LoadBitmapResize(File.DirAssets,"background.jpg",Activity.width / XUI.Scale,Activity.height / XUI.Scale ,True))
I understand perfectly. thank you. Without a doubt this is the solution.
You are creating a very large image. The memory required is roughly activity width * activity height * 4.

Start with eliminating the scale:
B4X:
Activity.SetBackgroundImage(LoadBitmapResize(File.DirAssets,"background.jpg",Activity.width / XUI.Scale,Activity.height / XUI.Scale ,True))
I understand perfectly. thank you. Without a doubt this is the solution.
 
Top