通过Wi-Fi Direct发送的图像损坏。

huangapple 未分类评论50阅读模式
英文:

Image sent over Wi-Fi Direct is corrupted

问题

以下是您要翻译的内容:

我正在尝试按照安卓文档中的这个教程来发送图片通过 Wi-Fi direct。基本上我只是复制了那里的内容,但是发送出去的却是一个损坏的大小相同的 JPEG 图片。数据成功地被发送并在另一端接收,但仍然生成了损坏的 JPEG 图片。我发送的图片绝对是 JPEG 格式的。这是当图像数据流在第二个设备上接收时的情况。

大部分代码都来自文档,所以我不明白为什么它不能工作。但是当我在安卓上查看文件时,它卡在加载中;而在 Windows 上,则显示“文件格式不受支持”。

这是将图像写入 OutputStream 的代码。

        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        try {

            OutputStream outputStream = socket.getOutputStream();
            ContentResolver cr = MainActivity.getContentResolver();
            InputStream inputStream = null;
            inputStream = cr.openInputStream(myUri);
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }



        } catch (IOException e) {
            e.printStackTrace();
        }


    }

这是接收图像文件字节并创建文件的代码。

                bytes = inputStream.read(buffer);

                tempMsg = new String(buffer);

                if ((tempMsg = null)) {
                    ...
                } else if (!isASCII(tempMsg)) {
                    final File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "mage.jpg");

                    File dirs = new File(f.getParent());
                    if (!dirs.exists())
                        dirs.mkdirs();
                    f.createNewFile();

                    copyFile(inputStream, new FileOutputStream(f));
英文:

I'm trying to follow this tutorial from the android docs which explains how to send images over Wi-Fi direct. I essentially just copied what was there and instead of the image, it sends a corrupted jpeg of the same size. The data is sent fine and it is received on the other end, but it still produces a corrupted jpeg. The images which I am sending are definitely jpeg format as well. This is what the image data stream looks like when it is received on the second device.

Most of this was taken from the documentation so I don't see why it wouldn't work, but when I view the file on Android, it gets stuck loading and on Windows, it says that "File format not supported".

This is the code responsible for writing the image to the OutputStream.

        OutputStream tmpOut = null;

        try {

            OutputStream outputStream = socket.getOutputStream();
            ContentResolver cr = MainActivity.getContentResolver();
            InputStream inputStream = null;
            inputStream = cr.openInputStream(myUri);
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }



        } catch (IOException e) {
            e.printStackTrace();
        }


    }

This is the code responsible for receiving the bytes of the image files and creating a file from it.

                bytes = inputStream.read(buffer);

                tempMsg = new String(buffer);

                if ((tempMsg = null)) {
                    ...
                } else if (!isASCII(tempMsg)) {
                    final File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "mage.jpg");

                    File dirs = new File(f.getParent());
                    if (!dirs.exists())
                        dirs.mkdirs();
                    f.createNewFile();

                    copyFile(inputStream, new FileOutputStream(f));

huangapple
  • 本文由 发表于 2020年7月26日 18:24:47
  • 转载请务必保留本文链接:https://java.coder-hub.com/63098885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定