iOS Question insert images into database

Albi

Active Member
Licensed User
Longtime User
Hello,

I'm trying to add a user generated image into a database.
I'm using the insertBlod method from here
https://www.b4x.com/android/forum/threads/sql-tutorial.6736/#content
And I'm using the B4i version of DBUtils (https://www.b4x.com/android/forum/threads/dbutils-android-databases-are-now-simple.8475/#content)

When saving the image, as there's only one image, I think i'm just adding one map so have this code. The first part is to save the image the user has taken, call it "image" and then use that in the inputstream later. I found that code on the forum and it seems to work.

B4X:
Sub btnSavePic_Click
    'save image to documents
    'save bitmap as "image", used in insertBlob
    Dim outPic As OutputStream
    outPic = File.OpenOutput(File.DirDocuments, "image", False)
    ImageView1.Bitmap.WriteToStream(outPic, 100, "png")
    outPic.close
   
    Dim listOfMaps As List
    listOfMaps.Initialize
   
    Dim m As Map
    m.Initialize
    m.Put("id", id)
    m.Put("pic", Array As Object(insertBlob)) 'insert bitmap as array of bytes
    listOfMaps.Add(m)
    DBUtils.InsertMaps(SQL, "pics", listOfMaps)
    id=id+1
    Log(id)
    kvs.PutSimple("id", id)
End Sub

Sub insertBlob As Object
    'convert the image file into a bytes array
    Dim inputStream1 As InputStream
    inputStream1 = File.OpenInput(File.DirDocuments, "image")
    Dim outputStream1 As OutputStream
    outputStream1.InitializeToBytesArray(1000)
    File.Copy2(inputStream1, outputStream1)
    Dim buffer() As Byte
    buffer = outputStream1.ToBytesArray
    Log(buffer.Length)
    Return buffer
End Sub

It tries to execute the SQL:
InsertMaps (first query out of 1): INSERT INTO [pics] ([pic], [id]) VALUES (?, ?)
taken from the logs in this line
B4X:
If i1 = 0 Then Log("InsertMaps (first query out of " & ListOfMaps.Size & "): " & sb.ToString)
so seems like the SQL is fine to me but gives this error message:

<B4IExceptionWrapper: Error Domain=caught_exception Code=0 "*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil" UserInfo=0x17548e80 {NSLocalizedDescription=*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil}>

I'm not sure what it means by object cannot be nil. As far as I can tell when debugging, the objects in InsertMaps of DBUtils are all as expected.

I've attached the project. Any help would be great!
 

Attachments

  • CameraMWE.zip
    199.5 KB · Views: 210

Erel

B4X founder
Staff member
Licensed User
Longtime User
The blob parameter should be an array of bytes. Your code creates an array of objects that holds an array of bytes:
B4X:
m.Put("pic", Array As Object(insertBlob))

You should change it to:
B4X:
m.Put("pic", insertBlob)

Note that it is better to explicitly set the return type of the insertBlob sub:
B4X:
Sub insertBlob As Byte()
 
Upvote 0
Top