2012-04-24 11 views
24

Attualmente ho due attività. Uno per tirare l'immagine dalla scheda SD e uno per la connessione Bluetooth.Immagine Uri to bytesarray

ho utilizzato un bundle per trasferire l'Uri dell'immagine dall'attività 1.

Ora quello che voglio fare è ottenere che Uri nell'attività Bluetooth per e convertirlo in uno stato trasmissibile tramite matrici di byte i ho visto alcuni esempi ma non riesco a farli funzionare per il mio codice !!

Bundle goTobluetooth = getIntent().getExtras(); 
    test = goTobluetooth.getString("ImageUri"); 

è quello che devo trascinare, quale sarebbe il prossimo passo?

Molte grazie

Jake

risposta

58

Da Uri per ottenere byte[] faccio le seguenti cose,

InputStream iStream = getContentResolver().openInputStream(uri); 
byte[] inputData = getBytes(iStream); 

e il metodo getBytes(InputStream) è:

public byte[] getBytes(InputStream inputStream) throws IOException { 
     ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); 
     int bufferSize = 1024; 
     byte[] buffer = new byte[bufferSize]; 

     int len = 0; 
     while ((len = inputStream.read(buffer)) != -1) { 
     byteBuffer.write(buffer, 0, len); 
     } 
     return byteBuffer.toByteArray(); 
    } 
+0

getContentResolver() openInputStream (test); sta ricevendo un errore dicendo che non è applicabile per gli argomenti per la stringa !! In riferimento al mio codice sopra i suoi stati, l'uri è in forma di stringa come faccio a cambiare questo in modo che coincida con il codice che hai indicato sopra! – user1314243

+0

test è una variabile stringa che devi passare Uri. Fai Uri da test e poi passa al metodo .. – user370305

+0

Uri uri = Uri.parse (test); Prova questo .. – user370305

0

uso getContentResolver() .openInputStream (uri) per ottenere un InputStream da un URI. e poi leggere i dati da InputStream convertire i dati in byte [] da quella InputStream

Prova con seguente codice

public byte[] readBytes(Uri uri) throws IOException { 
      // this dynamically extends to take the bytes you read 
     InputStream inputStream = getContentResolver().openInputStream(uri); 
      ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); 

      // this is storage overwritten on each iteration with bytes 
      int bufferSize = 1024; 
      byte[] buffer = new byte[bufferSize]; 

      // we need to know how may bytes were read to write them to the byteBuffer 
      int len = 0; 
      while ((len = inputStream.read(buffer)) != -1) { 
      byteBuffer.write(buffer, 0, len); 
      } 

      // and then we can return your byte array. 
      return byteBuffer.toByteArray(); 
     } 

consultare questo link

+8

Qual è la differenza tra la tua risposta e la mia? – user370305

+0

Il nome del metodo :) –

0

Questo codice funziona per me

Uri selectedImage = imageUri; 
      getContentResolver().notifyChange(selectedImage, null); 
      ImageView imageView = (ImageView) findViewById(R.id.imageView1); 
      ContentResolver cr = getContentResolver(); 
      Bitmap bitmap; 
      try { 
       bitmap = android.provider.MediaStore.Images.Media 
       .getBitmap(cr, selectedImage); 

       imageView.setImageBitmap(bitmap); 
       Toast.makeText(this, selectedImage.toString(), 
         Toast.LENGTH_LONG).show(); 
       finish(); 
      } catch (Exception e) { 
       Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT) 
         .show(); 

       e.printStackTrace(); 
      } 
0

Buone pratiche Java: non dimenticare mai di chiudere ogni flusso che apri! Questa è la mia realizzazione:.

/** 
* get bytes array from Uri. 
* 
* @param context current context. 
* @param uri uri fo the file to read. 
* @return a bytes array. 
* @throws IOException 
*/ 
public static byte[] getBytes(Context context, Uri uri) throws IOException { 
    InputStream iStream = context.getContentResolver().openInputStream(uri); 
    try { 
     return getBytes(iStream); 
    } finally { 
     // close the stream 
     try { 
      iStream.close(); 
     } catch (IOException ignored) { /* do nothing */ } 
    } 
} 



/** 
* get bytes from input stream. 
* 
* @param inputStream inputStream. 
* @return byte array read from the inputStream. 
* @throws IOException 
*/ 
public static byte[] getBytes(InputStream inputStream) throws IOException { 

    byte[] bytesResult = null; 
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); 
    int bufferSize = 1024; 
    byte[] buffer = new byte[bufferSize]; 
    try { 
     int len; 
     while ((len = inputStream.read(buffer)) != -1) { 
      byteBuffer.write(buffer, 0, len); 
     } 
     bytesResult = byteBuffer.toByteArray(); 
    } finally { 
     // close the stream 
     try{ byteBuffer.close(); } catch (IOException ignored){ /* do nothing */ } 
    } 
    return bytesResult; 
} 
0
public void uriToByteArray(String uri) 
    { 

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     FileInputStream fis = null; 
     try { 
      fis = new FileInputStream(new File(uri)); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     byte[] buf = new byte[1024]; 
     int n; 
     try { 
      while (-1 != (n = fis.read(buf))) 
       baos.write(buf, 0, n); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     byte[] bytes = baos.toByteArray(); 
    } 
Problemi correlati