1

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:

  1. Camera2 API to record the video - which gives videoURI and path and it saves the video in the internal storage

  2. 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;
Shiva
  • 11
  • 3
  • Welcome to Stackoverflow. Could you please add your source-code in how you try to upload the file at the moment? This would help us in finding a solution. – Nicola Uetz Jul 22 '19 at 20:37
  • Added the code, using the multipart request to send it to the server, the video bytes and other data related to the app in two parts respectively..but even before I start uploading to the server, I have got the video saved onto the device, which I don't want it to happen, thank you! – Shiva Jul 23 '19 at 13:22
  • good... and can you maybe append the code in how you record the video? Because in theory you will just write your bytes directly to your `ByteArrayOutputStream byteBuffer` or even better... Directly send it to your server. – Nicola Uetz Jul 23 '19 at 13:27

1 Answers1

0

You can try the method in the answer to this question.

Android record video into a circular buffer and storing it in RAM

Basically use JavaCV library to record in memory and then flush that memory aka upload it in chunks or whole depending on the size of your buffer and video recording limits.

Arnold Balliu
  • 1,099
  • 10
  • 21