1

我正在开发一个需要实现该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();
            }
            }

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

4

2 回答 2

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/

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

将位图保存到文件功能

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

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

于 2012-12-29T11:11:19.993 回答
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);
        }
于 2015-07-22T12:04:15.010 回答