图像 Uri 到字节数组 [英] Image Uri to bytesarray
问题描述
我目前有两项活动.一种用于从 SD 卡中提取图像,一种用于蓝牙连接.
I currently have two activities. One for pulling the image from the SD card and one for Bluetooth connection.
我使用了一个 Bundle 来传输来自活动 1 的图像的 Uri.
I have utilized a Bundle to transfer the Uri of the image from activity 1.
现在我想要做的是让蓝牙活动中的 Uri 并通过字节数组将其转换为可传输状态我已经看到了一些示例,但我似乎无法让它们为我的代码工作!!
Now what i wish to do is get that Uri in the Bluetooth activity to and convert it into a transmittable state via Byte Arrays i have seen some examples but i can't seem to get them to work for my code!!
Bundle goTobluetooth = getIntent().getExtras();
test = goTobluetooth.getString("ImageUri");
是我必须把它拉过去.下一步是什么?
is what i have to pull it across. What would be the next step?
推荐答案
从 Uri
到 byte[]
我做了以下事情,
From Uri
to get byte[]
I do the following things,
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();
}
这篇关于图像 Uri 到字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!