Android Question Insert jpg from phone to sql server... and parser.aspx question...

MarianoD

Member
Licensed User
Longtime User
Hello, please apologize my english...

I am using, from time to time, Basic4android VERSION 3.20. I AM NOT EXPERT!

When i need insert to a sql server database, or make a query to it, i use the PARSER.ASPX model, that i found here in forum, and works great.

My questions are:

- how can i send and jpg image, from the cell phone to my sql server? can i use something like ...parser.aspx?query=INSERT INTO MYTABLE....... in some way?

- what about "parser.aspx" model, do you think its too old? there is some easiest way in B4A 3.20 to conect to ms sql server, i meant something as "Dim Cn as conection... dim Rs as recordset"?

Thank you very much.
 

Bobi

Member
Licensed User
Longtime User
Please excuse my English too. A messy code from a very old application you can find below. Main idea is to convert binary image to string and then to parse it to web services (this part of code is not here but I understand you know how to do it). There is a problem: parsing as string parameter have size limitation; basically is something added to a internet address. Not bad but not perfect at all.

1st code extract image from camera and create a tem file
2nd code convert file to string
missing code of calling web services
3rd code is from web services (visual basic); convert back string to image. I send all parameters as one concatenated by "~".

Good luck.

P.S. Now my solution looks different because using MS SQL express edition (in my case) is not ok if you want to store big picture because of db size limitation. I just upload file with name identically with id of linked entity. Simple. For SQL 2008 and higher, standard and higher, you can use much more Pro SQL filestream.

B4X:
    Camera1.StartPreview
    out = File.OpenOutput(File.DirRootExternal, "tmp.jpg", False)
    out.WriteBytes(Data, 0, Data.Length)
    out.Close

        b = ResizeBitmap(LoadBitmap(File.DirRootExternal, "tmp.jpg"), 320, 240) ' size
        out = File.OpenOutput(File.DirRootExternal, index & ".jpg", False)
        b.WriteToStream(out, 100 , "JPEG")
        out.Close

B4X:
Sub WriteAsText(NumeFisier As String)
    Log(NumeFisier)
    Dim In As InputStream
    In = File.OpenInput(File.DirRootExternal, NumeFisier & ".jpg")
    Dim out As OutputStream
    out.InitializeToBytesArray(10000)
    File.Copy2(In, out)
    Dim data() As Byte
    data = out.ToBytesArray

    Dim su As StringUtils
    Dim PhotoAsString As String
   
    Dim url, encodedUrl As String
    encodedUrl = su.EncodeUrl(su.EncodeBase64(data), "UTF8")

    PhotoAsString = encodedUrl
   
    Dim dataLenght As Int=data.Length
InsertUsingWebServices(PhotoAsString)
End Sub

B4X:
    <WebMethod()>
       <ScriptMethod(UseHttpGet:=True)> _
    Public Function PRB_InsertCopieCI(PublicKey As String) As String
        Dim parts As String() = PublicKey.Split(New Char() {"~"c})
        If GetID_Device(parts(0)) = 0 Then
            Return "ERR"
            Exit Function
        End If
        Dim id_transact As Integer

        Dim PozaAsString As String
        Dim id_Comanda As Integer = CInt(parts(1))
        PozaAsString = CStr(parts(2))

        Dim command As SqlCommand
        Dim conn As New SqlConnection(connStringOAA)
        Dim da As SqlDataAdapter = Nothing
        Dim ds As DataSet = Nothing

        Dim binaryData() As Byte
        Try
            binaryData = System.Convert.FromBase64String(PozaAsString)
        Catch ex As Exception
            Log(ex.ToString)
        End Try
        conn.Open()
        Try
            command = New SqlCommand("PRB_InsertCopieCI", conn)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.AddWithValue("@id_Comanda", id_Comanda)
            command.Parameters.AddWithValue("@CopieCI", binaryData)
            command.ExecuteNonQuery().ToString()
            id_transact = Convert.ToInt32(command.ExecuteNonQuery())
        Catch ex As Exception
            Log(ex.ToString)
        Finally
            command = Nothing
            conn.Close()
        End Try
        Return id_transact
    End Function
 
Upvote 0

MarianoD

Member
Licensed User
Longtime User
Thank you bobi, i will take a look at your code...

Something that i dont say, is that if i can upload the jpg file, with some name related to primary key, its ok form me! Maybe HttpJob.PostFile?
I meant, if i have a record with primary key "1" and i can upload file named "1.jpg" to a folder on server (not to a field of a table) its ok for my.

Thanks.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…