I'm developing an android application where I record the video and upload it to the server, but I do not want to save the video on the device(either internal storage or on external storage), In iOS I have used AVFoundation and I could record the video and upload it to the server without saving it to the device.
I have used two ways to record the video in Android:
Camera2 API to record the video - which gives videoURI and path and it saves the video in the internal storage
Video capture Intent - this also returns the videoURI after saving the video to the device
But I'm working in healthcare app where I do not want to save the video at all on to the device, for now, I'm deleting the video after uploading it to the server, but sometimes the video remains in the internal storage of the app when the app is minimized or exited before the video is uploaded to the app, I'm looking for a better solution as to not save the video at all to the device, but get the video data to upload it to the backend
I'm setting up Media Recorder and setting the path as in where to save the video
final File dir = context.getExternalFilesDir(null);
mNextVideoUri = android.net.Uri.parse(String.valueOf(dir.toURI()));
return (dir == null ? "" : (dir.getAbsolutePath() + "/")) + System.currentTimeMillis() + ".nomedia";
//code to read the video bytes from the video uri I have got from the Camera API, and I used mutlipart request to upload it to the server
InputStream iStream = null;
try {
iStream = context1.getContentResolver().openInputStream(videoUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte[] bytesResult = null;
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1 * 1024 * 1024;
byte[] buffer = new byte[bufferSize];
try {
int len;
while ((len = iStream.read(buffer)) != -1) {
try {
byteBuffer.write(buffer, 0, len);
} catch (Exception e) {
e.printStackTrace();
}
}
bytesResult = byteBuffer.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
// close the stream
try {
byteBuffer.close();
} catch (IOException ignored) { /* do nothing */ }
}
videoBytes = bytesResult;