在Android中从输入流套接字检索JPEG图像时遇到问题

时间:2013-11-17 09:20:32

标签: android bytearray inputstream

我想从Android中的输入流套接字中检索JPEG图像。我可以实际使用Bitmap方法并直接将输入流转换为JPG,还是应该将输入流转换为字节数组然后将其转换为JPG?

我正在尝试的代码是:

            private final BluetoothSocket mmBTSocket;
    private final InputStream mmBTInStream;
    private final OutputStream mmBTOutStream;

    public ConnectedThread(BluetoothSocket socket) {
        Log.d(BTTAG, "create ConnectedThread");
        mmBTSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the BluetoothSocket input and output streams
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) {
            Log.e(BTTAG, "temp sockets not created", e);
        }

        mmBTInStream = tmpIn;
        mmBTOutStream = tmpOut;
    }
                      public void run() {

                      Log.i(BTTAG, "BEGIN mBTConnectedThread");

                        byte[] inBTBuffer = new byte[1024];

                         boolean BTFileEndOne = false;

                         byte[] mBTimageBuffer = new byte[15360]; // 15KB reserved

                               int bytes;

                                bytes = mmBTInStream.read(inBTBuffer);

                for (int i = 0; i < bytes; i++) {
                    mBTimageBuffer[mBTfileIndex] = inBTBuffer[i];
                    mBTfileIndex++;
                    // start = System.currentTimeMillis();
                    // Log.i(BTTAG, bytes+"="+String.format("%02X",
                    // inBTBuffer[i]));
                    if (i > 0) {
                        if (inBTBuffer[i] == (byte) 0xD9) {
                             BTFileEndOne = true;
                             Log.i(BTTAG, "BTFileEndOne = true");

                            if (inBTBuffer[i - 1] == (byte) 0xFF) {
                                //if (SaveImagetoSD() == true) {

                                    //byte[] readBuf = (byte[]) msg.obj;
        Bitmap bitmap = BitmapFactory.decodeStream(mmBTInStream);   
        bitmap.compress(CompressFormat.JPEG, 80, mmBTOutStream);

        String writeTo = new File(Environment.getExternalStorageDirectory()+ File.separator + "motoduino.jpg")  .toString();                   
         FileOutputStream output = null;  
         try {
            output = new FileOutputStream(writeTo);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }`

1 个答案:

答案 0 :(得分:1)

BitmapFactory可以加载png和jpg。

Bitmap bitmap = BitmapFactory.decodeStream(inputstream);

你可以保存为jpeg或png:

bitmap.compress(CompressFormat.JPEG, 80, outputstream);

编辑:保存到外部存储:

File sdcard = Environment.getExternalStorageDirectory();
    if(sdcard!=null){
        File myjpg = new File(sdcard,"test.jpg");
        try{
            OutputStream outputstream = new FileOutputStream(myjpg);
            mybitmap.compress(CompressFormat.JPEG, 80, outputstream);
            outputstream.close();
        }catch(Exception e){ e.printStackTrace(); }
    }else{
        Log.e("jpg save","sdcard not inserted or not ready");
    }

你的清单中需要使用权限android.permission.WRITE_EXTERNAL_STORAGE。