如何从蓝牙接收字节数组并将字节数组保存为SD卡作为图像文件

时间:2012-12-29 08:51:12

标签: android bluetooth

我正在开发一个需要实现Bluetooth功能的Android应用程序。它需要从外部h / w设备接收图像,然后将该字节数组保存为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();
            }
            }

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

2 个答案:

答案 0 :(得分:1)

这里似乎有多个问题。对于初学者来说,缓冲区尾部有垃圾,“设k是实际读取的字节数;这些字节将存储在元素b [0]到b [k-1]中,留下元素b [k]到b [b。长度-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)

你可以更容易地将这些构建块拼凑在一起,而不是重新发明它们。

答案 1 :(得分:1)

检查此解决方案;)

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