我目前有两项活动。一种用于从 SD 卡提取图像,一种用于蓝牙连接。
我使用了 Bundle 来传输活动 1 中图像的 Uri。
现在我想做的是获取蓝牙活动中的 Uri 并通过字节数组将其转换为可传输状态我已经看到了一些示例,但我似乎无法让它们为我的代码工作!
Bundle goTobluetooth = getIntent().getExtras();
test = goTobluetooth.getString("ImageUri");
这就是我必须把它拉过去的。下一步是什么?
从
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();
}
Kotlin 这里非常简洁:
@Throws(IOException::class)
private fun readBytes(context: Context, uri: Uri): ByteArray? =
context.contentResolver.openInputStream(uri)?.use { it.buffered().readBytes() }
Kotlin 为
InputStream
提供了方便的扩展功能,例如 buffered
、use
和 readBytes
。
buffered
将输入流装饰为 BufferedInputStream
use
处理关闭流readBytes
的主要工作是读取流并写入字节数组错误案例:
IOException
可能会在过程中发生(就像在 Java 中)openInputStream
可以返回null
。如果您在 Java 中调用该方法,您可以轻松地进行监督。想想你想如何处理这个案子。kotlin 中的语法
val inputData = contentResolver.openInputStream(uri)?.readBytes()
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;
}
使用 getContentResolver().openInputStream(uri) 从 URI 获取 InputStream。然后从输入流读取数据,将数据从该输入流转换为 byte[]
尝试使用以下代码
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();
}
参考此链接
此代码对我有用
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();
}
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();
}
使用以下方法在Android studio中从
bytesArray
创建URI
。
public byte[] getBytesArrayFromURI(Uri uri) {
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
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();
}catch(Exception e) {
Log.d("exception", "Oops! Something went wrong.");
}
return null;
}
一个解决方案,关闭字节流并照顾你的记忆:
fun Uri.readBytes(context: Context): ByteArray? {
var inputStream : InputStream? = null
var byteArrayOutputStream: ByteArrayOutputStream? = null
try {
inputStream = context.contentResolver.openInputStream(this)
byteArrayOutputStream = ByteArrayOutputStream()
if (inputStream != null) {
val buffer = ByteArray(1024)
var bytesRead: Int
while (inputStream.read(buffer).also { bytesRead = it } != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead)
}
return byteArrayOutputStream.toByteArray()
}
} catch (e: IOException) {
e.printStackTrace()
} finally {
inputStream?.close()
byteArrayOutputStream?.close()
}
return null
}