2012-04-24 74 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(); 
    }