2013-03-15 88 views
2

我已經修改了Android藍牙聊天示例應用程序以現在發送圖像。這對第一張圖片來說很好。它被髮送並正確顯示。當我嘗試發送另一張圖像時,它似乎會將前一張圖像發送20次以上,此時應該只發送一次新圖像。我嘗試過使用oef,但無濟於事。Android - 通過藍牙以編程方式發送圖像

這會發送圖片:

 Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.rc_a); 

     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     icon.compress(Bitmap.CompressFormat.JPEG, 40, bytes); 
     byte[] image = bytes.toByteArray(); 

     mConnection.write(image); 

這是ConnectedThread:

public void run() { 
     byte[] buffer = new byte[1024]; 
     byte[] imgBuffer = new byte[1024 * 1024]; 
     int pos = 0; 

     // Keep listening to the InputStream while connected 
     while (true) { 
      try { 
       // Read from the InputStream 
       int bytes = mmInStream.read(buffer); 
       System.arraycopy(buffer,0,imgBuffer,pos,bytes); 
       pos += bytes; 

       mHandler.obtainMessage(BtoothSetupActivity.MESSAGE_READ, 
         pos, -1, imgBuffer).sendToTarget(); 

      } catch (IOException e) { 
       connectionLost(); 
       break; 
      } 
     } 
    } 

該讀取數據回:

case MESSAGE_READ: 
    byte[] readBuf = (byte[]) msg.obj; 
    Bitmap bmp = BitmapFactory.decodeByteArray(readBuf, 0, msg.arg1); 

回答

1

用於傳送文件,你可以做一個明確呼籲ACTION_SEND使用意圖。

使用ACTION_SEND,將彈出一個菜單,其中包含可處理要發送的文件類型的應用程序,用戶需要從中選擇藍牙,然後選擇要發送該文件的設備。

File sourceFile = new File("//mnt/sdcard/file.apk"); 
Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_SEND); 
Intent.setType("image/jpeg"); 
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(sourceFile)); 
startActivity(intent); 

附加幫助:

+0

我們的想法是,以控制另一個照相機的裝置(其被實現),然後在發送捕獲的圖像。所以理想情況下,沒有人與相機設備進行交互(僅用於設置應用程序)。 – mr0mr 2013-03-15 19:26:17

+0

請參閱提供的鏈接以獲取有關您問題的更多幫助。 – syb0rg 2013-03-15 19:53:08