我现在有两个活动。一个从SD卡中提取图像,另一个用于蓝牙连接。
我已经利用Bundle传递了第一个活动中图像的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() }
context.contentResolver.openInputStream(uri)?.use { it.buffered().readBytes() }
,这样即使缓冲区出现问题(虽然不太可能),您也可以确保流被关闭。 - rtsketoKotlin中的语法
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;
}
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();
}
请参考此链接
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中从URI创建一个bytesArray
。
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;
}
这段代码对我有效
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();
}
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
}
InputStream iStream =...
这一行代码出现了FileNotFoundException
异常。 - mrid