图像Uri到bytesarray

use*_*243 38 android uri bytearray

我目前有两项活动.一个用于从SD卡中提取图像,一个用于蓝牙连接.

我利用Bundle从活动1转移图像的Uri.

现在我希望做的就是该URI的蓝牙活动,它通过字节数组转换为传递状态我已经看到了一些例子,但我似乎无法让他们为我的代码工作!

Bundle goTobluetooth = getIntent().getExtras();
    test = goTobluetooth.getString("ImageUri");
Run Code Online (Sandbox Code Playgroud)

是我必须把它拉过来,下一步会是什么?

非常感谢

可靠的人

use*_*305 78

Uribyte[]我做下面的事情,

InputStream iStream =   getContentResolver().openInputStream(uri);
byte[] inputData = getBytes(iStream);
Run Code Online (Sandbox Code Playgroud)

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();
    }
Run Code Online (Sandbox Code Playgroud)

  • InputStream iStream = ...`行给了我`FileNotFoundException` (2认同)

Pet*_*r F 14

Kotlin is very concise here:

@Throws(IOException::class)
private fun readBytes(context: Context, uri: Uri): ByteArray? = 
    context.contentResolver.openInputStream(uri)?.buffered()?.use { it.readBytes() }
Run Code Online (Sandbox Code Playgroud)

In Kotlin, they added convenient extension functions for InputStream like buffered,use , and readBytes.

  • buffered decorates the input stream as BufferedInputStream
  • use handles closing the stream
  • readBytes does the main job of reading the stream and writing into a byte array

Error cases:

  • IOException can occur during the process (like in Java)
  • openInputStream can return null. If you call the method in Java you can easily oversee this. Think about how you want to handle this case.

  • 我认为最好改为 `context.contentResolver.openInputStream(uri)?.use { it.buffered().readBytes() }`,这样即使缓冲区以某种方式失败(尽管不太可能),您也可以确保流关闭)。 (3认同)

小智 9

科特林语法

val inputData = contentResolver.openInputStream(uri)?.readBytes()
Run Code Online (Sandbox Code Playgroud)


ahm*_*_89 5

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;
}
Run Code Online (Sandbox Code Playgroud)