【问题标题】:How to receive byte array from Bluetooth and save byte array to SD card as an image file如何从蓝牙接收字节数组并将字节数组作为图像文件保存到 SD 卡
【发布时间】:2012-12-29 08:51:11
【问题描述】:

我正在开发一个需要实现Bluetooth 功能的Android 应用程序。它需要从外部硬件设备接收图像,然后将该字节数组作为图像文件保存到 SD 卡中。

这是我的代码:

public void run(){
                Log.i(TAG, "BEGIN mConnectedThread");
                byte[] buffer = new byte[4096];

            //CREATE IMAGE FILE
            File test=new File(Environment.getExternalStorageDirectory(), "test.jpg");

            if (test.exists()) {
                test.delete();
            }

            try {
                fos = new FileOutputStream(test.getPath());
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);

                    /*mEmulatorView.write(buffer, bytes);
                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(BlueTerm.MESSAGE_READ, bytes, -1, buffer).sendToTarget();*/
                    //receivedData = new String(buffer);

                    receivedData=new String(buffer)

                        //WRITE BYTES TO FILE
                        try {

                            fos.write(buffer);

                        }
                        catch (java.io.IOException e) {
                            Log.e("PictureDemo", "Exception in photoCallback", e);
                        }

                        /*
                        Bitmap bitmap = null;
                        try {
                            bitmap = BitmapFactory.decodeByteArray(buffer , 0, buffer.length);
                        } catch (OutOfMemoryError e) {
                            e.printStackTrace();
                        }catch (Exception e) {
                            e.printStackTrace();
                        }
                        if(bitmap != null)

                                testModeOnScreen.saveBitmapToSdcardAndEmail(bytes, bitmap);
                         */}

                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    break;
                }
            }
            try {
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }

我的问题是我没有收到任何异常或崩溃,但我收到的图像文件已损坏。

【问题讨论】:

  • 您确定在使用蓝牙传输数据时获得正确的尺寸吗?

标签: android bluetooth


【解决方案1】:

这里似乎有多个问题。对于初学者来说,缓冲区尾部的垃圾,“设 k 为实际读取的字节数;这些字节将存储在元素 b[0] 到 b[k-1] 中,留下元素 b[k] 到 b[b]。 length-1] 不受影响”:

http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html#read()

另外,您可能碰巧需要使用 Android 的位图功能。

看看他们如何在这里交换位图:

http://kamrana.wordpress.com/2012/05/12/sending-images-over-bluetooth-in-android/

以及它们如何保存到文件中:

Save bitmap to file function

http://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream)

您会发现将这些构建块拼凑在一起比重新发明它们更容易。

【讨论】:

  • 感谢您的回复,但我仍然无法创建图像文件。
【解决方案2】:

检查此解决方案;)

String TAG="IMG";
        File path = Environment.getExternalStoragePublicDirectory(("/mnt/exSdCard/RecievedIMAGE"));
        File file = new File(path, "test.jpg");
        BufferedInputStream bufferedInputStream = null;

        try {
            // Make sure that this path existe if it is not so create it
            path.mkdirs();

            bufferedInputStream = new BufferedInputStream(inputStream);
            OutputStream os = new FileOutputStream(file);

            final byte[] buffer = new byte[2048];
            int available = 0;

            while ((available = bufferedInputStream.read(buffer)) >= 0) {
                os.write(buffer, 0, available);
            }
            bufferedInputStream.read(buffer);


            bufferedInputStream.close();
            os.close();
            Log.d(TAG, "success" + buffer);

        } catch (IOException e) {
            // Unable to create file, likely because external storage is
            // not currently mounted.
            Log.w("ExternalStorage", "Error writing " + file, e);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多