Is there any library in kmp can do convert from by...
# multiplatform
h
Is there any library in kmp can do convert from byteArray into file? Then this file I will upload using ktor client.
z
You can use okio to do this
h
@zt but it doesn't show how to convert from byteArray into file
m
Is this what you are looking for?
Copy code
fun okio.Path.writeBytes(content: ByteArray) = okio.FileSystem.SYSTEM.write(this) { write(content) }

fun okio.Path.readBytes(): ByteArray = okio.FileSystem.SYSTEM.read(this) { return readByteArray() }
h
okio.FileSystem.SYSTEM.write(this). what is this refer too?
r
Do you need to save the file? The client will convert the file back into a byte array.
h
I want convert from byte array into file. let say I got this value only:
Copy code
@Serializable
data class ProfilePictureRequestUpdate(val imageArray: ByteArray)
How to do that?
c
You don't need to convert to a file first before uploading. You can upload a byte array
h
@Caleb Kiage yeah I do this :
Copy code
client.put{
    url (queryUrl + pathProfileUpdate)
    setBody( MultiPartFormDataContent(
            formData {
                append(
                    key = keyUploadProfilePicture,
                    value = profilePicture.imageArray
                )
            }
        )
    )
}.body()
but the server return 500
h
@Robert Jaros the problem is that I didn't know what is image type and fileName. I only have byteArray/ImageBitmap that's all
c
Multipart form data is for files I think, octet-stream is for bytes (but the server has to allow it)
r
Just make up a string for filename.
See how file. ReadBytes just returns the byte array so you don't need to save the file
h
yeah but the headers required contentType and contentDisposition. so in this case how do i get this information from byteArray?
c
Try
image/bmp
for a bitmap image
You should know the type if you're showing it on your app
h
I use third party library to capture image camera and gallery. that library just provided imageBitmap and byteArray only. By the way I try but server return 505 with message request to long? what does meaning of this? when test upload in postman type is file no have issue.
r
You can convert the bitmap to png in memory on android there are loads of examples online.
Will save bandwidth
s
Capture the server request on a tool like Wireshark (or any other that can monitor http traffic). Look at the successful one from Postman and the failing one from Android and examine the differences.
(just read the youtrack ticket and someone recommends doing the same ^^^)
And do you upload (.imageArray) the PNG/jpg data, not the raw ARGB data of the bitmap?
h
I have upload imageArray but server return 500 request too long. Second time, I got respond "No update data provided" which return 500 error code. @streetsofboston
@Robert Jaros the problem is that mine just kotlin multiplatform and not only android.
316 Views