2012-12-29 39 views
1

我正在研究需要实现Bluetooth功能的Android应用程序。它需要从外部硬件设备接收图像,然后将该字节数组保存为SD卡作为图像文件。如何从蓝牙接收字节数组并将字节数组保存为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(); 
      } 
      } 

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

+0

你确保你能获得正确的尺寸,同时使用蓝牙transfering数据? – hardartcore

回答

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)

你可以发现它更容易黑客在一起的而不是重新发明它们。

+0

感谢您的回复,但我仍然无法创建图像文件。 –

0

检查该解决方案;)

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); 
     } 
相关问题