Android Question If BLOB is NULL

Declan

Well-Known Member
Licensed User
Longtime User
I am reading a SQLite database BLOB field.
If the BLOB is NULL then I want to display an image from DirAssets.

I get an error:
B4X:
Error occurred on line: 1600 (HomePage)
java.lang.RuntimeException: Error loading bitmap.
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:521)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    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.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at com.rob.ebuki.homepage.afterFirstLayout(homepage.java:103)
    at com.rob.ebuki.homepage.access$000(homepage.java:18)
    at com.rob.ebuki.homepage$WaitForLayout.run(homepage.java:81)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5631)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
** Activity (homepage) Resume **
My code for reading from the SQLite db and displaying the image:
B4X:
        If Curs.GetBlob("userpic") = Null Then
            Dim Buffer() As Byte
            Dim InStream As InputStream
            InStream = File.OpenInput(File.DirAssets,"touchphotogrey.png")
            Dim OutputStream1 As OutputStream
            OutputStream1.InitializeToBytesArray(100000)
            File.Copy2(InStream, OutputStream1)
            Buffer = OutputStream1.ToBytesArray
            OutputStream1.Close
              
            Dim InputStream1 As InputStream
            InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
            Dim Bitmap1 As Bitmap
            Bitmap1.Initialize2(InputStream1)
            InputStream1.Close
            Dim cvs As Canvas = CreateBitmap
            DrawRoundBitmap(cvs, Bitmap1)
            imgUser.SetBackgroundImage(cvs.Bitmap)
            imgUserInfo.SetBackgroundImage(cvs.Bitmap)
        End If
               
        If Curs.GetBlob("userpic") <> Null Then
            Dim Buffer() As Byte
            Buffer = Curs.GetBlob("userpic")
            Dim InputStream1 As InputStream
            InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
              
            Dim Bitmap1 As Bitmap
            Bitmap1.Initialize2(InputStream1) '<< ERROR HERE
            InputStream1.Close
            Dim cvs As Canvas = CreateBitmap
            DrawRoundBitmap(cvs, Bitmap1)
            imgUser.SetBackgroundImage(cvs.Bitmap)
            imgUserInfo.SetBackgroundImage(cvs.Bitmap)
        End If
        Curs.Close
 

imbault

Well-Known Member
Licensed User
Longtime User
You should try something like this, in order to check your blob is not empty
B4X:
If Curs.GetBlob("userpic")<> Null And Curs.GetBlob("userpic").Length >1 Then
 
Upvote 0
Top