Java 通过蓝牙从android向PC发送图像

Java 通过蓝牙从android向PC发送图像,java,android,bluetooth,Java,Android,Bluetooth,我正在制作一个应用程序,将图像从android设备发送到PC上运行的java应用程序。客户端(android)上的图像是位图,我将其转换为字节数组,以便通过蓝牙将其发送到服务器 ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] b = baos.toByteArray();

我正在制作一个应用程序,将图像从android设备发送到PC上运行的java应用程序。客户端(android)上的图像是
位图
,我将其转换为
字节数组
,以便通过蓝牙将其发送到服务器

 ByteArrayOutputStream baos = new ByteArrayOutputStream();  
 ImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);     
 byte[] b = baos.toByteArray();
 mBluetoothService.write(b);
请注意,位图来自已压缩的文件,因此我不需要再次压缩它

我在服务器上使用以下代码(Java):

客户端没有错误。但我在服务器端(java应用程序)遇到了这个异常:


java.lang.IllegalArgumentException:im==null

在javax.imageio.imageio.write(未知源)

在javax.imageio.imageio.write(未知源)

在com.luugiathuy.apps.remotebluetooth.ProcessConnectionThread.run(ProcessConnectionThread.java:68)

位于java.lang.Thread.run(未知源)

因此,
ImageIO.read()
没有返回任何内容。它似乎无法将字节数组识别为图像。我在互联网上搜索过,但没有什么能帮我解决这个问题。有人知道吗


非常感谢

我终于明白了!客户端(Android)创建了一个接收线程和一个写入线程。所以,当我发送一个图像时,它是分块发送的。EAndroid操作系统时不时地暂停写入线程,因此服务器端(Java应用程序)的inputStream看到的是图像是碎片。因此,
ImageIO.read()
没有成功读取图像,而是读取图像的一部分,这就是为什么我得到“java.lang.IllegalArgumentException:im==null!”的原因,因为不能仅使用块创建图像

解决方案:

除了图像之外,我还向服务器发送一个“文件结束”字符串,以便它知道文件何时完成(我想有更好的方法来实现这一点,但这是可行的)。在服务器端,在while循环中,我接收所有字节块并将它们放在一起,直到收到“文件结束”。守则:

Android客户端:

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);     
    byte[] b = baos.toByteArray();
    mBluetoothService.write(b);
    mBluetoothService.write("end of file".getBytes());
Java服务器:

    byte[] buffer = new byte[1024];

    File f = new File("c:/users/temp.jpg");
    FileOutputStream fos = new FileOutputStream (f);

    int bytes = 0;
    boolean eof = false;

    while (!eof) {

        bytes = inputStream.read(buffer);
        int offset = bytes - 11;
        byte[] eofByte = new byte[11];
        eofByte = Arrays.copyOfRange(buffer, offset, bytes);
        String message = new String(eofByte, 0, 11);

        if(message.equals("end of file")) {

            eof = true;

        } else {

            fos.write (buffer, 0, bytes);

        }

    }
    fos.close();

希望它能帮助别人。

java.lang.IllegalArgumentException:im==null!在javax.imageio.imageio.write(未知源代码)在javax.imageio.imageio.write(未知源代码)在com.luugiathuy.apps.remotebluetooth.ProcessConnectionRead.run(ProcessConnectionRead.java:68)在java.lang.Thread.run(未知源代码)编辑您的问题并发布整个日志,以便社区成员可以帮助您,而不仅仅是其中的一部分
    byte[] buffer = new byte[1024];

    File f = new File("c:/users/temp.jpg");
    FileOutputStream fos = new FileOutputStream (f);

    int bytes = 0;
    boolean eof = false;

    while (!eof) {

        bytes = inputStream.read(buffer);
        int offset = bytes - 11;
        byte[] eofByte = new byte[11];
        eofByte = Arrays.copyOfRange(buffer, offset, bytes);
        String message = new String(eofByte, 0, 11);

        if(message.equals("end of file")) {

            eof = true;

        } else {

            fos.write (buffer, 0, bytes);

        }

    }
    fos.close();