如何从android发送PNG文件并在python中接收它

2024-10-05 12:22:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将PNG文件从android服务器发送到python客户端。
我尝试发送的PNG图像是一个屏幕截图,大约4mb,通常低于2mb。在

android代码(发送):

                File myFile = new File(imagePath);
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(myFile);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                BufferedInputStream bis = new BufferedInputStream(fis);

                Log.i("service", "sending file len");
                try {
                    out.write("" +myFile.length());
                    out.flush();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                Log.i("service:", "waiteing for ok");
                try {
                    msg = in.readLine();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                Log.i("service", "sending file");
                byte[] outBuffer = new byte[(int) myFile.length()];
                try {
                    bis.read(outBuffer, 0, outBuffer.length);
                    os = client.getOutputStream();
                    os.write(outBuffer, 0, outBuffer.length);
                } catch (IOException e) {
                    e.printStackTrace();
                }

python代码(接收):

^{pr2}$

文件从android传输到我的电脑,但文件已损坏。
当我将其与原始图像进行比较时,几乎所有内容都是相同的
除了这里和那里的一些空行(不是缺少信息,只是像有人添加的空行)和1或2大块行(15行左右)丢失。
Here您可以看到这些文件之间的比较(左为原始文件,右为发送后的文件)。
我不知道为什么文件传输损坏了,请帮帮我。在


Tags: 文件代码图像lognewpngservicemyfile

热门问题