Streaming a file from an Android phone SD card while camera is still writing to file
<p>I am writing an application that uses the MediaRecorder to write video to a file on the SD card.  While the file is still being written to, I want to stream the contents of the file to a web site.  I was not having any luck reading the contents of the file using BufferedInputStream, all the data was "0".  I found a post on this site:</p>

<p><a href="http://stackoverflow.com/questions/1471072/how-to-split-file-into-chunks-while-still-writing-into-it">How to split file into chunks while still writing into it?</a></p>

<p>This showed an example of using FileChannel to open the input file and reading into a ByteBuffer.  I tried this and I am getting data in the buffer that I read.  However, the data cannot be read as a video file.  It appears that I cannot successfully read the video stream unless the input file is closed.</p>

<p>As a test, I saved a video file (.3gp) to the SD card and stopped the recorder, closing the file.  I was then able to successfully read the file in chunks and send them to a web site, where they were reassembled and the full video could be played.</p>

<p>I then used the same code but started reading the file before the recorder had finished recording and closing the file.  I sent the chunks up to the web site but the reassembled video file could not be read.  It seems that when I try to read the file while it is still open for writing the data gets corrupted.  I looked at the documentation for MediaRecorder to see if there was some way to set the file access to Read/Write but I did not find anything.</p>

<p>Here is the code I am using.  This is in a loop and I have a check to make sure I only read the amount of data that is available in the input file.  Any help would be greatly appreciated.</p>

<pre><code>File inputFile = new File(localFileUri); // The original file on the SD card
FileInputStream inputFileStream = new FileInputStream(inputFile);
FileChannel inputFileChannel = inputFileStream.getChannel();
ByteBuffer inputBuffer = null;

inputBuffer = ByteBuffer.allocate(10000);
byte[] bucket;
inputFileChannel.read(inputBuffer);
inputBuffer.flip();
bucket = inputBuffer.array();
inputBuffer.clear();

resultFile = new FileOutputStream(destinationFile);
resultFile.write(bucket);
resultFile.flush();
resultFile.close();
</code></pre>
