Android Question error when i try to read code

Makumbi

Well-Known Member
Licensed User
B4X:
Dim Buffer() As Byte 'declare an empty byte array
        Buffer =  cursor1.GetBlob("image")
 
        Dim IpSt As InputStream
        Dim Bitmap1 As Bitmap

        IpSt.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
    
        Bitmap1.Initialize2(IpSt)
        IpSt.Close

        ImageView1.SetBitmap(CreateRoundBitmap(Bitmap1, ImageView1.Width))

the error comes when it reaches this line
B4X:
   Bitmap1.Initialize2(IpSt)

below is the error that comes up
B4X:
Student Names
19-07561
Error occurred on line: 411 (Curveextracts)
java.lang.RuntimeException: Error loading bitmap.
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:539)
    at b4a.example.curveextracts$ResumableSub_ReadBlob.resume(curveextracts.java:1672)
    at b4a.example.curveextracts._readblob(curveextracts.java:1267)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1764)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 

Makumbi

Well-Known Member
Licensed User
The problem is not in this code. It is probably in the code that inserts the bitmap to the database.
this is my code that inserts bitmap to database and it has been work well
B4X:
Sub SavePhoto (NewFile As String)
    'Dim Buffer() As Byte 'declare an empty byte array
    'Buffer = Base64StringToImage(NewFile)
    Dim bmp As Bitmap = (CreateRoundBitmap(Base64EncodeDecodeImage.Base64StringToImage(NewFile), ImageView1.Width))

    
    B4XPages.MainPage.SQL.ExecNonQuery2("INSERT INTO Studentphoto2 VALUES('smiley', ?, ?)", Array As Object(Base64EncodeDecodeImage.Base64ImageToString2(bmp),Account.Text))
    ImageView1.SetBitmap(CreateRoundBitmap(bmp, ImageView1.Width))
    
    'Main.sql_conexion.ExecNonQuery2("Update Clientes Set Imagen = ? where Codigo = ?",Array As Object(buffer1,Cliente_Codigo))
    'File.Delete(File.DirRootExternal, NewFile)
    
    
'    Starter.SQL1.ExecNonQuery2("INSERT INTO Studentphoto2 VALUES('smiley', ?, ?)", Array As Object(NewFile,Account.Text))
    'ImageView1.SetBitmap(CreateRoundBitmap(Base64StringToImage(NewFile), ImageView1.Width))
    
End Sub

Sub CreateRoundBitmap (Input As B4XBitmap, Size As Int) As B4XBitmap
    If Input.Width <> Input.Height Then
        'if the image is not square then we crop it to be a square.
        Dim l As Int = Min(Input.Width, Input.Height)
        Input = Input.Crop(Input.Width / 2 - l / 2, Input.Height / 2 - l / 2, l, l)
    End If
    Dim c As B4XCanvas
    Dim xview As B4XView = xui.CreatePanel("")
    xview.SetLayoutAnimated(0, 0, 0, Size, Size)
    c.Initialize(xview)
    Dim path As B4XPath
    path.InitializeOval(c.TargetRect)
    c.ClipPath(path)
    c.DrawBitmap(Input.Resize(Size, Size, False), c.TargetRect)
    c.RemoveClip
    c.DrawCircle(c.TargetRect.CenterX, c.TargetRect.CenterY, c.TargetRect.Width / 2 - 2dip, xui.Color_White, False, 5dip) 'comment this line to remove the border
    c.Invalidate
    Dim res As B4XBitmap = c.CreateBitmap
    c.Release
    Return res
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Very inefficient code.
2. You are mistakenly inserting a base64 string to the database.

B4X:
Dim su As StringUtils
Dim b() As Byte = su.DecodeBase64(NewFile) 'I assume that NewFile is a base 64 string
Dim bmp As Bitmap = BytesToImage(b) 'https://www.b4x.com/android/forum/threads/b4x-bytes-to-file.70111/post-445167
B4XPages.MainPage.SQL.ExecNonQuery2("INSERT INTO Studentphoto2 VALUES('smiley', ?, ?)", Array(b ,Account.Text))
 
Upvote 0
Top