2016-04-28 6 views
1

私はthis Androidサンプルをガイドとして使用して、何の検証もせずにブルートゥース接続を作成しました。このアプリケーションには非常に制限されたユーザーベースがあり、格納)。bluetoothで転送されたバイトをマージする

文字列をうまく伝えることができ、魅力的に機能します。私の問題は画像を転送しようとするときです。

私は、イメージのバイト[]をブルートゥースサービスに送信する1つのアクティビティと、メッセージを受信して​​そのメッセージを借りて行う他のアクティビティのハンドラを持っています。

問題は、バッファのサイズのために、ハンドラが元のバイト[]の部分を受け取ることです。私がしようとしていることは、すべての部分を1バイトでマージして保存することです。

これは私が私のハンドラ内で行うループです:

byte[] result = new byte[originalByteSize]; 
byte[] readBuf = (byte[]) msg.obj; 

if (cont < byteTimes){ 
    if (result == null) { 
     result = appendData(readBuf,readBuf); 
    } else { 
     result = appendData(result,readBuf);   
    } 
} else { 
    new SavePhotoTask(cont).execute(result); 
} 

これは

protected byte[] appendData(byte[] firstObject,byte[] secondObject){ 
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
    try { 
     if (firstObject!=null && firstObject.length!=0) 
      outputStream.write(firstObject); 
     if (secondObject!=null && secondObject.length!=0) 
      outputStream.write(secondObject); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return outputStream.toByteArray(); 
} 

appendData関数であり、ここで私は、ファイルの書き込み場所です:

public class SavePhotoTask extends AsyncTask<byte[], String, String> { 

    int counter = 0; 

    public SavePhotoTask(int cont){ 
     this.counter = cont; 
    } 

    @Override 
    protected String doInBackground(byte[]... jpeg) { 
     File photo = new File(Environment.getExternalStorageDirectory(), counter + "_photo.jpg"); 

     if (photo.exists()) { 
      photo.delete(); 
     } 

     try { 
      FileOutputStream fos = new FileOutputStream(photo.getPath()); 

      fos.write(jpeg[0]); 
      fos.close(); 
     } catch (java.io.IOException e) { 
      Log.e("PictureDemo", "Exception in photoCallback", e); 
     } 

     return (null); 
    } 

を私が必要としたのはちょうど正しい方向のヒントです、ありがとう。

+0

このコードの問題点は何ですか? –

+0

@pratt出力する画像は黒い画面です – Carlos

+0

コードはうまく見えますが、コードをデバッグする必要があります –

答えて

0

私はこの問題は、私は、ストリームを書いて読んでいた道にあったこのanswer

と私の問題を解決しました。

public static void writeItem(OutputStream out, String s) throws IOException 
{ 
    // Get the array of bytes for the string item: 
    byte[] bs = s.getBytes(); // as bytes 
    // Encapsulate by sending first the total length on 4 bytes : 
    // - bits 7..0 of length 
    out.write(bs.length);  // modulo 256 done by write method 
    // - bits 15..8 of length 
    out.write(bs.length>>>8); // modulo 256 done by write method 
    // - bits 23..16 of length 
    out.write(bs.length>>>16); // modulo 256 done by write method 
    // - bits 31..24 of length 
    out.write(bs.length>>>24); // modulo 256 done by write method 
    // Write the array content now: 
    out.write(bs); // Send the bytes 
    out.flush(); 
} 

public static String readItem(InputStream in) throws IOException 
{ 
    // first, read the total length on 4 bytes 
    // - if first byte is missing, end of stream reached 
    int len = in.read(); // 1 byte 
    if (len<0) throw new IOException("end of stream"); 
    // - the other 3 bytes of length are mandatory 
    for(int i=1;i<4;i++) // need 3 more bytes: 
    { 
     int n = in.read(); 
     if (n<0) throw new IOException("partial data"); 
     len |= n << (i<<3); // shift by 8,16,24 
    } 
    // Create the array to receive len bytes: 
    byte[] bs = new byte[len]; 
    // Read the len bytes into the created array 
    int ofs = 0; 
    while (len>0) // while there is some byte to read 
    { 
     int n = in.read(bs, ofs, len); // number of bytes actually read 
     if (n<0) throw new IOException("partial data"); 
     ofs += n; // update offset 
     len -= n; // update remaining number of bytes to read 
    } 
    // Transform bytes into String item: 
    return new String(bs); 
} 
関連する問題