Android Question error when convert byte to image

azzam223

Active Member
Licensed User
Longtime User
hello i have load image from sql server database and insert it in sql lite database and when i convert the image from byte to bitmap i got this error

B4X:
java.lang.OutOfMemoryError
    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:528)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:600)
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:519)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:753)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:343)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:259)
    at b4a.example.frmfood._displyfood(frmfood.java:476)
    at b4a.example.frmfood._generatelist(frmfood.java:417)
    at b4a.example.frmfood._activity_create(frmfood.java:385)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:340)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at b4a.example.frmfood.afterFirstLayout(frmfood.java:102)
    at b4a.example.frmfood.access$000(frmfood.java:17)
    at b4a.example.frmfood$WaitForLayout.run(frmfood.java:80)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
Error occurred on line: 41
java.lang.RuntimeException: Object should first be initialized (Bitmap).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at b4a.example.frmfood.afterFirstLayout(frmfood.java:102)
    at b4a.example.frmfood.access$000(frmfood.java:17)
    at b4a.example.frmfood$WaitForLayout.run(frmfood.java:80)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
An error occurred:
(Line: 101) End Sub
java.lang.RuntimeException: Array not expected...
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

azzam223

Active Member
Licensed User
Longtime User
thanks donmanfred

but also when image size is 500*500 the same error happened what is limit of it dimension
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Are you loading many images?

There is no hard limit. It depends on the process available memory.

A better way to load many images or images with unknown sizes is to save the data to a temporary file and then load it with LoadBitmapSample. It has two advantages:
1. It will downsample the image based on the provided width and height.
2. If there is not enough memory then it will downsample the image multiple times to avoid the process from crashing.
 
Upvote 0

azzam223

Active Member
Licensed User
Longtime User
thanks erel but when i save image in file and load it with LoadBitmapSample

the error happend

error loading bitmap

i use this code to save image in file

B4X:
Public Sub Createfile(img() As Byte) As String
    Try
        Dim b As Bitmap
        Dim in As InputStream
        in.InitializeFromBytesArray(img,0,img.Length)
        b.Initialize2(in)
        Dim out As OutputStream
        Dim filename As Int=StateManager.GetSetting2("filename",0)
        filename=filename+1
        out=File.OpenOutput(File.DirDefaultExternal,$"${filename}.png"$,False)
        b.WriteToStream(out,100,"PNG")
StateManager.SetSetting("filename",0)
StateManager.SaveSettings
Return filename & ".png"
 
Last edited:
Upvote 0
Top