java - Android FileOutputStream 似乎失败

标签 java android file sockets

我正在尝试通过 WiFi 将视频文件从 RPi 热点传输到手机上的目录。我已经能够在存储中成功创建文件夹、连接 RPi 服务器并接收数据。但是写入后出现的文件不正确。事实上,当我尝试打开它时,它只是在我的手机上打开一个单独的、不相关的应用程序。很奇怪!

这是有问题的代码:

 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();
        }

视频最初采用 .h264 格式,并作为字节数组发送。该文件大小为 10MB。在我的 while 循环中,我将数组的值作为字符串打印出来,并且它打印了大量数据。足够的数据让我怀疑所有数据都已发送。当我导航到它应该位于的文件夹时,有一个文件的名称是我给它的“recording1”,但它的大小只有 8KB。

对正在发生的事情有什么想法吗?任何帮助是极大的赞赏!

最佳答案

Android FileOutputStream seems to fail

不,没有。您的代码似乎失败了。那是因为你的代码没有意义。您正在丢弃大量数据,或多或少只累积了每 8192 字节中的 1 个;您同时使用缓冲和非缓冲读取;您将输入限制为 8192 字节;而且你永远不会关闭输入。如果输入大于 8192*8193,您可能会收到 ArrayIndexOutOfBoundsException

把它全部扔掉并使用这个:

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();
    }

关于java - Android FileOutputStream 似乎失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42756131/

相关文章:

java - Kotlin 泛型 : Trouble migrating from Java

java - 在 android 服务中使用前置摄像头拍照

python - 如果两个 python 脚本想写在同一个文件中会发生什么?

python - 文件对象中的rb和r+b模式有什么区别

java - Groovy中HashMap的点积

java - 从 Android 编写的文本文件中读取数值

android - 微调器 OnItemSelected 不起作用

java - 在不同的 Activity 中使用 Nav Drawer 设置

python - 值错误: dictionary update sequence element #0 has length 1; 2 is required while reading from file

Java - 将 Yaml 解析为 Json