please see my below code which I use to upload a video to a server. However, for large enough videos, I'm getting an OutOfMemory exception.
InputStream stream = getContentResolver().openInputStream(videoUri);
byte[] byteArray = IOUtils.toByteArray(stream);
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", "fname",
RequestBody.create(MediaType.parse("video/mp4"), byteArray))
.build();
Request request = new Request.Builder()
.url(uploadURL)
.post(requestBody)
.build();
OkHttpClient client = new OkHttpClient.Builder().build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
}
});
Can someone point me in the right direction on how to avoid OutOfMemory exception? Is there a way I can go from the InputStream to the requestBody?