2012-04-24 24 views
24

Mam obecnie dwie czynności. Jeden do wyciągania obrazu z karty SD i jeden do połączenia Bluetooth.Obraz Uri do bytesarray

mam wykorzystane Bundle przenieść URI obrazka z działalności 1.

Teraz co chcę zrobić, to że Uri aktywności Bluetooth i przekształcić go w stan transmitowanej przez tablicami bajtów i widziałem kilka przykładów, ale nie mogę sprawić, żeby działały dla mojego kodu !!

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

jest to, co muszę zrobić, co będzie następnym krokiem?

Wiele Dzięki

Jake

Odpowiedz

58

Od Uri dostać byte[] robię następujące rzeczy,

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

i metoda getBytes(InputStream) jest:

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); otrzymuje komunikat o błędzie informujący, że nie ma zastosowania do argumentów dla ciągu znaków !! W odniesieniu do mojego kodu powyżej jego stanów uri jest w formie ciągów, jak to zmienić, aby pokryć się z kodem, który podałeś powyżej! – user1314243

+0

test jest zmienną łańcuchową, którą musisz przekazać Uri. Zrób Uri z testu, a następnie przekaż go do metody. – user370305

+0

Uri uri = Uri.parse (test); Wypróbuj to .. – user370305

0

użycie getContentResolver() .openInputStream (uri), aby uzyskać InputStream z URI. a następnie odczytać dane z InputStream konwertować dane w byte [] z tego InputStream

spróbuj następujący kod

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

Patrz to linki

+8

Jaka jest różnica między twoją odpowiedzią a moją? – user370305

+0

Nazwa metody :) –

0

ten kod działa dla mnie

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

Najlepsza praktyka Java: nigdy nie zapomnij zamknąć każdego strumienia, który otworzysz! To moja realizacja.

/** 
* 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(); 
    } 
Powiązane problemy