I have the following lines in my app in a class that acts as AsyncTask
to include an image in the body of a query HTTP
.
I have the following object File
:
val file = File(getContext().cacheDir, "fileName")
file.createNewFile()
and then with a FileOutputStream
I write a BitMap
in that file in such a way:
val fos = FileOutputStream(file)
fos.write(bitMapData)
fos.flush()
fos.close()
And finally, to write all the bytes in the request
of the query, I use:
val bytes = Files.readAllBytes(file.toPath())
request.write(bytes) //La variable request es el OutputStream del objeto HttpUrlConnection
These two functions, readAllBytes()
and toPath()
ask me for a minimum API of 26, which is very high, which would be your substitute for smaller APIs.