Short description how to transfer a jpg from Android to a .NET-Service using POST
Hi there,
just in case one do have the same task, here is a quick description to achive a result.
Open the file and fetch it into a CharArray
Transform it to a byte Array
transform it into a URL-Safe base64 encoded string
put the parameter name of the Webservice as plain text & "=" , join the string and use it as parameter for webservice.InitializePost2 (this will transform the string to an UTF-8 Byte array
start the webservice. On the .NET side it will appear as string.
in the webService
Create a standard base64 string (undo the URLSAFE transcription (replace all - by + and all _ by /).
transform the string into a byte array
transform the byte array into a memory stream
create a image from the memory stream
save the image as file
if you want to store the jpg in a database, you first have to transofrm it into a byte array.
Sound complex and indeed, it is. It took me some hours to find this way.
Here the main steps as code snippets:
sb.Append ("Pic=") 'Name of the WebServiceParameter
Dim in As InputStream
Dim out As OutputStream
in = File.OpenInput(PicturePath, PictureName)
out.InitializeToBytesArray(0)
File.Copy2(in, out)
Dim data() As Byte
data = out.ToBytesArray
Dim b64 As Base64
b64.BreakLines = True
b64.UrlSafe = True
b64.LineLength = 75
Dim str64Pic As String
str64Pic = b64.EncodeBtoS(data,0,data.Length)
sb.Append(str64Pic) 'Append the urlsafe base64 coded string
...
---- WebService
request.InitializePost2(URL,conv.StringToBytes(sb.tostring,"UTF-8"))
...
...
---- At the .NET side
Dim r As Byte() = Convert.FromBase64String(StringBase64.Replace("-", "+").Replace("_", "/")) 'Bild in Originalgröße als Bytestream
Dim ms As New MemoryStream(r)
Dim img As Image = Image.FromStream(ms)
Dim rOut As Byte()
Dim imgOut As Image
Dim msOut As New MemoryStream()
imgOut = img.GetThumbnailImage(1024, 768, myCallback, IntPtr.Zero)
imgOut.Save(msOut, System.Drawing.Imaging.ImageFormat.Jpeg)
rOut = msOut.ToArray()
rNewWundePosBild.Bild = rOut 'Update database