iOS Question insert images into database

Discussion in 'iOS Questions' started by Albi, May 28, 2015.

  1. Albi

    Albi Active Member Licensed User

    Hello,

    I'm trying to add a user generated image into a database.
    I'm using the insertBlod method from here
    http://www.b4x.com/android/forum/threads/sql-tutorial.6736/#content
    And I'm using the B4i version of DBUtils (http://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.

    Code:
    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
    Code:
    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!
     

    Attached Files:

  2. Erel

    Erel Administrator Staff Member Licensed User

    The blob parameter should be an array of bytes. Your code creates an array of objects that holds an array of bytes:
    Code:
    m.Put("pic"Array As Object(insertBlob))
    You should change it to:
    Code:
    m.Put("pic", insertBlob)
    Note that it is better to explicitly set the return type of the insertBlob sub:
    Code:
    Sub insertBlob As Byte()
     
    Mark Turney and Albi like this.
  3. Albi

    Albi Active Member Licensed User

    thank you kindly.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice