2012-04-24 78 views
24

我目前有兩項活動。一個用於從SD卡拉取圖像,一個用於藍牙連接。Image Uri to bytesarray

我已經利用一個包從活動傳輸的圖像的URI 1

現在我希望做的就是該URI的藍牙活動,它通過字節數組轉換爲可以發射狀態i已經看到了一些例子,但我似乎無法讓他們爲我的代碼工作!

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

是我必須把它拉過來,下一步是什麼?

非常感謝

傑克

回答

58

Uri得到byte[]我做以下事情,

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

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(試驗);收到一個錯誤,說它不適用於字符串的參數!在我的代碼上面提到它的狀態uri是以字符串形式表示的,我如何改變這個以符合你上面陳述的代碼! – user1314243 2012-04-24 12:05:41

+0

test是一個字符串變量,你必須通過Uri。使Uri從測試中,然後將其傳遞給方法.. – user370305 2012-04-24 12:06:41

+0

Uri uri = Uri.parse(test);試試這個.. – user370305 2012-04-24 12:13:09

0

使用getContentResolver() .openInputStream(URI )從URI獲取InputStream。然後讀取InputStream中的數據的數據轉換爲字節[]從輸入流

嘗試用下面的代碼

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

請參閱此鏈接

+8

你的答案和我的區別有什麼區別? – user370305 2012-04-24 12:08:27

+0

方法的名稱:) – 2016-03-24 14:18:23

0

此代碼對我的作品

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

Java最佳實踐:永遠不要忘記關閉你打開的每一個流! 這是我實現:

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