2016-04-28 12 views
1

übertragen wurde So habe ich this Android Probe als Leitfaden verwendet, um eine Bluetooth - Verbindung ohne irgendeine Art von Validierung (Diese App wird eine sehr Benutzerbasis einschränken und wird nicht zum Download zur Verfügung stehen Geschäft).Zusammenführen von Byte [], das über Bluetooth

Ich konnte String sehr gut übertragen, und es funktioniert wie ein Charme. Mein Problem ist beim Übertragen von Bildern.

Ich habe eine Aktivität, die das Byte [] des Bildes an den Bluetooth-Dienst sendet und einen Handler für die andere Aktivität, die die Nachricht erhält und Wharever mit der besagten Nachricht ausführt.

Die Sache ist, wegen der Größe des Puffers empfängt der Handler Teile des ursprünglichen Bytes []. Was ich versuche, ist, alle Teile in einem Byte zusammenzuführen und zu speichern.

Dies ist die Schleife, die ich in meinem Handler tun:

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); 
} 

Dies ist die appendData Funktion

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(); 
} 

Und hier ist, wo ich die Datei schreiben:

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); 
    } 

Was ich brauchte, ist nur ein Tipp in die richtige Richtung, danke.

+0

Also, was ist das Problem in diesem Code? –

+0

@ pratt das Bild, das nur einen schwarzen Bildschirm ausgibt – Carlos

+0

Code sieht gut aus, muss den Code debuggen, denke ich –

Antwort

0

löste ich mein Problem mit diesem answer

Das Problem in der Art und Weise war ich schrieb und den Stream zu lesen.

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); 
}