我目前有两项活动。一种用于从 SD 卡提取图像,另一种用于蓝牙连接。
我使用 Bundle 来传输活动 1 中图像的 Uri。
现在我想做的是获取蓝牙活动中的 Uri 并通过字节数组将其转换为可传输状态我已经看到了一些示例,但我似乎无法让它们为我的代码工作!
Bundle goTobluetooth = getIntent().getExtras();
test = goTobluetooth.getString("ImageUri");
这就是我必须克服的。下一步会怎样?
From Uri
to get byte[]
我做以下事情,
InputStream iStream = getContentResolver().openInputStream(uri);
byte[] inputData = getBytes(iStream);
and the 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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)