I have the following lines in my app in a class that acts as if 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 request
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()
they ask me for a minimum API of 26, which is very high, what would be their substitute for smaller APIs.
As you well comment Files.readAllBytes() was added from API 26.
You can configure your project with:
compileSdkVersion=26
YtargetSdkVersion=26
If you want code for older APIs :