2013-03-12 53 views
-1

我正在使用Android。我從圖像文件中獲取字節數組。我的要求是發送一個字節類型變量到服務器,而不是字節數組。如何將byte []數組轉換爲字節類型變量?

但我得到字節數組,然後我想將該字節數組轉換爲字節變量。

我的代碼是這樣

InputStream in = getContentResolver().openInputStream(selectedImageUri); 
    byte[] imageData = readBytes(in); 

    byte bytedata = // here I want get the bytes from imageData array 


public byte[] readBytes(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); 

     } 

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

你可以將它轉換成字符串bytearay.toString();並通過網絡 – Pragnani 2013-03-12 07:27:41

+0

發送它,如果您可以發送一個字節,請嘗試通過網絡逐個發送字節流。 – 2013-03-12 09:08:25

回答

1

你如何指望字節數組轉換爲一個字節?這就像把n件東西轉換成一件東西。您可以嘗試逐個發送數組的元素,因爲它們是字節類型的,但除此之外,我不知道如何實現您的嘗試。

+1

OP正在尋找最終的壓縮... – Selvin 2013-03-12 07:50:32

+0

@Selvin好,然後讓OP通過網絡發送42,因爲我們知道42是最終的答案 – 2013-03-12 07:52:53

相關問題