Gigaxalus
Gigaxalus

Reputation: 107

Android FileOutputStream Seems to Fail

I am trying to transfer a video file from an RPi hotspot to my a directory on my phone over WiFi. I have been able to successfully create a folder in my storage, connect with the RPi server, and receive data. However, the file that appears after being written isn't correct. In fact, when I try to open it, it just opens a separate, unrelated app on my phone. Very weird!

Here is the code in question:

 try {
            BufferedInputStream myBis = new BufferedInputStream(mySocket.getInputStream());
            DataInputStream myDis = new DataInputStream(myBis);

            byte[] videoBuffer = new byte[4096*2];
            int i = 0;

            while (mySocket.getInputStream().read(videoBuffer) != -1) {
                Log.d(debugStr, "while loop");
                videoBuffer[videoBuffer.length-1-i] = myDis.readByte();
                Log.d(debugStr, Arrays.toString(videoBuffer));
                i++;
            }

            Log.d(debugStr, "done with while loop");
            // create a File object for the parent directory

            File testDirectory = new File(Environment.getExternalStorageDirectory()+File.separator, "recordFolder");
            Log.d(debugStr, "path made?");
            if(!testDirectory.exists()){
                testDirectory.mkdirs();
            }
            Log.d(debugStr, "directory made");
            // create a File object for the output file
            File outputFile = new File(testDirectory.getPath(), "recording1");

            Log.d(debugStr, "outputfile made");
            // now attach the OutputStream to the file object, i

            FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
            Log.d(debugStr, "write to file object made");


            fileOutputStream.write(videoBuffer);
            Log.d(debugStr, "video written");
            fileOutputStream.close();

            Log.d(debugStr, "done");
        } catch (IOException e1) {
            e1.printStackTrace();
        }

The video is initially in .h264 format and is being sent as a byte array. The file is 10MB in size. In my while loop, I print out the value of the array as a string, and it prints a lot of data. Enough data for me to suspect that all the data is being sent. When I navigate to the folder it should be in, there is a file with the name I gave it, "recording1", but it is only 8KB in size.

Any ideas on what is going on? Any help is greatly appreciated!

Upvotes: 0

Views: 1020

Answers (1)

user207421
user207421

Reputation: 310860

Android FileOutputStream seems to fail

No it doesn't. Your code seems to fail. That's because your code makes no sense. You're throwing away large chunks of data, more or less accumulating only 1 out of every 8192 bytes; you're using both buffered and unbuffered reads; you're limiting the input to 8192 bytes; and you're never closing the input. And if the input is larger than 8192*8193 you can get an ArrayIndexOutOfBoundsException.

Throw it all away and use this:

try {
        File testDirectory = new File(Environment.getExternalStorageDirectory()+File.separator, "recordFolder");
        if(!testDirectory.exists()){
            testDirectory.mkdirs();
        }
        File outputFile = new File(testDirectory, "recording1");
        try (OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile));
            BufferedInputStream in = new BufferedInputStream(mySocket.getInputStream())) {
            byte[] buffer = new byte[8192]; // or more, whatever you like > 0
            int count;
            // Canonical Java copy loop
            while ((count = in.read(buffer)) > 0)
            {
                out.write(buffer, 0, count);
            }
        }
    } catch (IOException e1) {
        e1.printStackTrace();
    }

Upvotes: 3

Related Questions